我试图删除日期字符串中的序号。
我需要验证序数之前至少有一个数字,这样我们就知道它是序数而不是单词的一部分。这是正确的正则表达式:
/(?:\d)(st|nd|rd|th)/g
现在,当我在Javascript中对字符串进行正则表达式替换时,我最终在序号之前替换了前导数字,这是#34;捕获的"我的非捕获组也可以在这里看到:
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>
&#13;
代码段不起作用?检查 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;
&#13;
如您所见,我的非捕获组被忽略了。这是为什么我的string.replace
也忽略了我的捕获组?如果是这样,那么我应该如何替换&#34;序数&#34;在日期字符串中并验证Javascript中是否有前导数字(当然还有前导数字)?谢谢!
更新:以下是已接受的正则表达式的片段
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);
&#13;
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;
}
&#13;
<input id="str_data" value="The 1st, 2nd, 3rd, and 4th" />
<button id="swap_btn" class="btn btn-primary" >
Swap Text
</button>
&#13;
答案 0 :(得分:10)
使用捕获组并替换为$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);
答案 1 :(得分:2)
当您将regext传递给.match()
并且正则表达式具有g
选项(全局)时,match的返回值是所有完整匹配的数组;这些组不会被退回,只有完整的匹配。 JavaScript不会忽略您的非捕获组(也不是您的捕获组),但由于g
标记,您只是无法获得有关它们的任何信息。