【发布时间】: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