【问题标题】:Javascript Replace Using Regex With A Non-Capturing GroupJavascript 使用带有非捕获组的正则表达式替换
【发布时间】:2016-01-13 18:11:56
【问题描述】:

我正在尝试删除日期字符串中的序数。

我需要验证在序数之前至少有一个数字,这样我们就知道它是一个序数而不是单词的一部分。这是正确的正则表达式:

/(?:\d)(st|nd|rd|th)/g

现在,当我在 Javascript 中对字符串进行正则表达式替换时,我最终会替换我的非捕获组“捕获”的序数之前的前导数字,您可以在此处看到:

var inpt;

function swapText()
{
  var str = inpt.value;
  var reg = /(?:\d)(st|nd|rd|th)/g;

  str = str.replace(reg, "");
  
  inpt.value = str;
}

function init()
{
  inpt = document.getElementById('str_data');
  var btn = document.getElementById('swap_btn');
  btn.addEventListener('click', swapText, false);
}

setTimeout(init, 0);
body {
  font:13.23px "Open Sans", Verdana, sans-serif;
}

input {
  min-height:30px;
  height:auto;
  width:auto;
  padding: 6px 8px;
  color: #424242;
}

.btn {
	display: inline-block;
	padding: 8px 12px;
	margin-bottom: 0;
	font-size: 14px;
	font-weight: 500;
	line-height: 1.428571429;
	text-align: center;
	white-space: nowrap;
	vertical-align: middle;
	cursor: pointer;
	border: 1px solid transparent;
	border-radius: 4px;
	-webkit-user-select: none;
	-moz-user-select: none;
	-ms-user-select: none;
	-o-user-select: none;
	user-select: none;
}

.btn-success {
	color: #fff;
	background-color: #5cb85c;
	border-color: #4cae4c;
}

.btn-primary {
    color: #fff;
    background-color: #337ab7;
    border-color: #2e6da4;
}

input, button, select, textarea {
  font-family: inherit;
  font-size: inherit;
  line-height: inherit;
}

button, html input[type="button"], input[type="reset"], input[type="submit"] {
  cursor: pointer;
  -webkit-appearance: button;
}

button, select {
  text-transform: none;
}
<input id="str_data" value="The 1st, 2nd, 3rd, and 4th" />
<button id="swap_btn" class="btn btn-primary" >
  Swap Text
</button>

代码 sn-p 不起作用?检查 this JSFiddle

现在,在浏览了建议的匹配问题后,我发现在某些语言中,非捕获组在正则表达式匹配中会被忽略。 Javascript是这种情况吗?

例如,如果我有字符串 The 1st, 2nd, 3rd, and 4th 并且我要使用上面提供的正则表达式运行 string.match,这将是我的输出:

var str = "The 1st, 2nd, 3rd, and 4th";
var opt = JSON.stringify(str.match(/(?:\d)(st|nd|rd|th)/g));
document.body.innerHTML = opt;

如您所见,我的非捕获组被忽略了。这就是我的string.replace 也忽略我的捕获组的原因吗?如果是这样,那么我应该如何替换日期字符串中的“序数”并验证 Javascript 中是否有前导数字(当然要保留前导数字)?谢谢!

更新:这是一个接受正则表达式的 sn-p

var inpt;

function swapText()
{
  var str = inpt.value;
  var reg = /(\d)(?:st|nd|rd|th)/g;

  str = str.replace(reg, "$1");
  
  inpt.value = str;
}

function init()
{
  inpt = document.getElementById('str_data');
  var btn = document.getElementById('swap_btn');
  btn.addEventListener('click', swapText, false);
}

setTimeout(init, 0);
body {
  font:13.23px "Open Sans", Verdana, sans-serif;
}

input {
  min-height:30px;
  height:auto;
  width:auto;
  padding: 6px 8px;
  color: #424242;
}

.btn {
	display: inline-block;
	padding: 8px 12px;
	margin-bottom: 0;
	font-size: 14px;
	font-weight: 500;
	line-height: 1.428571429;
	text-align: center;
	white-space: nowrap;
	vertical-align: middle;
	cursor: pointer;
	border: 1px solid transparent;
	border-radius: 4px;
	-webkit-user-select: none;
	-moz-user-select: none;
	-ms-user-select: none;
	-o-user-select: none;
	user-select: none;
}

.btn-success {
	color: #fff;
	background-color: #5cb85c;
	border-color: #4cae4c;
}

.btn-primary {
    color: #fff;
    background-color: #337ab7;
    border-color: #2e6da4;
}

input, button, select, textarea {
  font-family: inherit;
  font-size: inherit;
  line-height: inherit;
}

button, html input[type="button"], input[type="reset"], input[type="submit"] {
  cursor: pointer;
  -webkit-appearance: button;
}

button, select {
  text-transform: none;
}
<input id="str_data" value="The 1st, 2nd, 3rd, and 4th" />
<button id="swap_btn" class="btn btn-primary" >
  Swap Text
</button>

【问题讨论】:

  • 你的意思是获取this
  • JavaScript 不会忽略非捕获组。
  • 是的@stribizhev 我做到了!这太妙了!谢谢!
  • vks 发布了相同的答案。
  • 好的,就是这样:在正则表达式中有匹配和不匹配。当您捕获模式的一部分时(使用()),此值将保存在内存缓冲区中。因此,捕获组也用作分组和子值保存结构。非捕获组 ((?:...)) 仅 group (适用于替代或可选序列定义),不保存任何子值。替换的经验法则是匹配并捕获您需要获取的内容,仅匹配您不需要的内容

标签: javascript regex


【解决方案1】:

使用捕获组并替换为$1。使用替换而不是匹配。

(\d)(?:st|nd|rd|th)

查看演示。

https://regex101.com/r/iJ7bT6/6

var re = /(\d)(?:st|nd|rd|th)/g; 
var str = 'The 1st, 2nd, 3rd, and 4th';
var subst = '$1'; 

var result = str.replace(re, subst);

【讨论】:

    【解决方案2】:

    当您将正则表达式传递给.match() 并且正则表达式具有g 选项(全局)时,匹配的返回值是所有完整匹配项的数组;不返回组,只返回完整的匹配项。 JavaScript 不会忽略您的非捕获组(也不会忽略您的捕获组),但是由于 g 标志,您只是无法获得有关它们的任何信息。

    【讨论】:

      【解决方案3】:

      url中的变量日期替换

      posutochnaja-arenda-nedvizhimost-v-yalte/zhk-morskoy-spusk-v-yalte-id20879?date_from_to=01.12.2021-03.12.2021&clear_cache=Y&_r=3298#rooms-header-block
      

      https://regex101.com/r/fgjGuo/1

      【讨论】:

        猜你喜欢
        • 2019-01-27
        • 1970-01-01
        • 2014-02-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-02
        相关资源
        最近更新 更多