我正在尝试删除日期字符串中的序数.
我需要验证序数之前至少有一个数字,这样我们就知道它是序数而不是单词的一部分.这是正确的正则表达式:
/(?:\d)(st|nd|rd|th)/g
Run Code Online (Sandbox Code Playgroud)
现在,当我在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);
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
<input id="str_data" value="The 1st, 2nd, 3rd, and 4th" />
<button id="swap_btn" class="btn btn-primary" >
Swap Text
</button>
Run Code Online (Sandbox Code Playgroud)
代码段不起作用?检查 这个JSFiddle.
现在,在探讨建议的匹配问题之后,我发现在某些语言中,在正则表达式匹配中忽略非捕获组.这是Javascript的情况吗?
例如,如果我有字符串,The 1st, 2nd, 3rd, and 4th
并且我string.match
使用上面提供的正则表达式运行a ,那么这将是我的输出:
var str = "The 1st, 2nd, 3rd, and 4th";
var opt = JSON.stringify(str.match(/(?:\d)(st|nd|rd|th)/g));
document.body.innerHTML = opt;
Run Code Online (Sandbox Code Playgroud)
如您所见,我的非捕获组被忽略了.这是为什么我也string.replace
忽略了我的捕获组?如果是这样,那么我应该如何替换日期字符串中的"序数"并验证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);
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
<input id="str_data" value="The 1st, 2nd, 3rd, and 4th" />
<button id="swap_btn" class="btn btn-primary" >
Swap Text
</button>
Run Code Online (Sandbox Code Playgroud)
vks*_*vks 10
使用捕获组并替换为$1
.use replace而不是match.
(\d)(?:st|nd|rd|th)
Run Code Online (Sandbox Code Playgroud)
见演示.
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);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2335 次 |
最近记录: |