admin管理员组文章数量:1026373
I need to wrap the two last characters in a string in a separate <span>
:
-1:23 // This is what I have
-1:<span>2</span><span>3</span> // This is what I want
The following matches the last character – but how can I make it match the second last as well?
str.replace(/(.$)/, "<span>$1</span>");
Thanks :)
I need to wrap the two last characters in a string in a separate <span>
:
-1:23 // This is what I have
-1:<span>2</span><span>3</span> // This is what I want
The following matches the last character – but how can I make it match the second last as well?
str.replace(/(.$)/, "<span>$1</span>");
Thanks :)
Share Improve this question asked Jul 8, 2018 at 9:22 Tonald DrumpTonald Drump 1,3961 gold badge21 silver badges38 bronze badges 02 Answers
Reset to default 3You may use
.replace(/.(?=.?$)/g, "<span>$&</span>")
See the regex demo
If these must be digits, replace .
with \d
:
.replace(/\d(?=\d?$)/g, "<span>$&</span>")
The pattern matches
\d
- a digit(?=\d?$)
- that is followed with an end of string or a digit and end of string.
The $&
is a replacement backreference that references the whole match value from the string replacement pattern.
JS demo:
console.log("-1:23".replace(/.(?=.?$)/g, "<span>$&</span>"));
console.log("-1:23".replace(/\d(?=\d?$)/g, "<span>$&</span>"));
Now, to make it more dynamic, you may use a limiting (range/interval) quantifier:
function wrap_chars(text, num_chars) {
var reg = new RegExp(".(?=.{0," + (num_chars-1) + "}$)", "g");
return text.replace(reg, "<span>$&</span>");
}
console.log(wrap_chars("-1:23", 1)); // wrap one char at the end with span
console.log(wrap_chars("-1:23", 2)); // wrap two chars at the end with span
You can add another group before the last one, which also matches a single character ((.)
), then wrap each of them using references ($1
and $2
):
var str = '-1:23'.replace(/(.)(.)$/, '<span>$1</span><span>$2</span>')
console.log(str);
I need to wrap the two last characters in a string in a separate <span>
:
-1:23 // This is what I have
-1:<span>2</span><span>3</span> // This is what I want
The following matches the last character – but how can I make it match the second last as well?
str.replace(/(.$)/, "<span>$1</span>");
Thanks :)
I need to wrap the two last characters in a string in a separate <span>
:
-1:23 // This is what I have
-1:<span>2</span><span>3</span> // This is what I want
The following matches the last character – but how can I make it match the second last as well?
str.replace(/(.$)/, "<span>$1</span>");
Thanks :)
Share Improve this question asked Jul 8, 2018 at 9:22 Tonald DrumpTonald Drump 1,3961 gold badge21 silver badges38 bronze badges 02 Answers
Reset to default 3You may use
.replace(/.(?=.?$)/g, "<span>$&</span>")
See the regex demo
If these must be digits, replace .
with \d
:
.replace(/\d(?=\d?$)/g, "<span>$&</span>")
The pattern matches
\d
- a digit(?=\d?$)
- that is followed with an end of string or a digit and end of string.
The $&
is a replacement backreference that references the whole match value from the string replacement pattern.
JS demo:
console.log("-1:23".replace(/.(?=.?$)/g, "<span>$&</span>"));
console.log("-1:23".replace(/\d(?=\d?$)/g, "<span>$&</span>"));
Now, to make it more dynamic, you may use a limiting (range/interval) quantifier:
function wrap_chars(text, num_chars) {
var reg = new RegExp(".(?=.{0," + (num_chars-1) + "}$)", "g");
return text.replace(reg, "<span>$&</span>");
}
console.log(wrap_chars("-1:23", 1)); // wrap one char at the end with span
console.log(wrap_chars("-1:23", 2)); // wrap two chars at the end with span
You can add another group before the last one, which also matches a single character ((.)
), then wrap each of them using references ($1
and $2
):
var str = '-1:23'.replace(/(.)(.)$/, '<span>$1</span><span>$2</span>')
console.log(str);
本文标签: javascriptRegex match last and secondlast characterStack Overflow
版权声明:本文标题:javascript - Regex: match last and second-last character - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745643414a2160881.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论