我想要替换紧跟在a-z A-Z 0-9。-_换行空间旁边的数字,它们正好在a-z A-Z 0-9之前.- 但不是2个空格或2个其他语言字符之间的那些。
我有像:
这样的字符串 1. "www.asdf123asd.com"
2. "asd123.sdf"
3. "123.sdf"
4. "adf 123 asdf"
我希望将它们更改为:
1. "www.asdf<b>123</b>asd.com"
2. "asd<b>123</b>.sdf"
3. "<b>123</b>.sdf"
4. "adf 123 asdf"
任何建议?
答案 0 :(得分:2)
您可以使用:
repl = string.replace(/(^|[a-z_.-])(\d+)(?=[a-z_.-])/ig, "$1<b>$2</b>");
<强>测试:强>
> "www.asdf123.com".replace(/(^|[a-z_.-])(\d+)(?=[a-z_.-])/ig, "$1<b>$2</b>");
"www.asdf<b>123</b>.com"
> "www.asdf 123 asd.com".replace(/(^|[a-z_.-])(\d+)(?=[a-z_.-])/ig, "$1<b>$2</b>");
"www.asdf 123 asd.com"
> "123.sdf".replace(/(^|[\w.-])(\d+)(?=[\w.-])/g, "$1<b>$2</b>");
"<b>123</b>.sdf"