Javascript替换字符串regex
Javascript replace string regex
我有以下字符串
hello[code:1], world [code:2], hello world
我想将此字符串替换为
hello <a href="someurl/1">someothercode</a>, world <a href="someurl/2">someothercode</a>, hello world
我想要这个字符串转换使用javascript reg ex
我试过
/'[code:('d+)']/
reg ex,但不确定如何标记它们
这能满足您的需求吗?
mystring.replace(/'[code:([0-9]+)']/g, '<a href="someurl/$1">somelink</a>');
演示
var s = 'hello[code:1], world [code:2], hello world';
var codes = {
1: '<a href="someurl/1">someothercode</a>',
2: '<a href="someurl/2">someothercode</a>'
};
s = s.replace(/'[code:('d*)']/g, function(a, b) {
return codes[b]
})
document.write(s)
您还不完全清楚自己想要什么,但这会输出您想要的结果:
"hello[code:1], world [code:2], hello world"
.replace(/'[(.*?):(.*?)']/g, '<a href="someurl/$2">someother$1</a>')
输出:
'hello<a href="someurl/1">someothercode</a>, world <a href="someurl/2">someothercode</a>, hello world'
相关文章:
- 制作一个regex来验证只有一个数字的字符串
- Datatables查询url字符串:需要regex来修复url解析
- 用于查找子字符串的Regex
- Regex,用于从字符串中筛选关键字
- RegEx只匹配JSON字符串中最里面的数组
- JavaScript Regex(?i)选项中字符串大小写不敏感的部分不起作用
- RegEx以匹配字符串中的两个或多个名称
- Regex模式匹配,从Javascript中的字符串中提取时间,结果出乎意料
- JavaScript Regex,用于由特定字符串包围的数字和空格
- regex:字符串中的最小数字总数
- 返回javascript中regex字符串的一部分
- Javascript RegEx - 字符串异常的开始和结束
- 将JavaScript Regex字符串转换为PHP Regex字符串
- Javascript用随机数替换Regex-字符串
- Javascript Regex字符串替换不替换
- Javascript Regex:字符串中的9或12位数字
- JavaScript RegEx字符串结尾不起作用
- regex字符串显示一个错误
- 带有空格的Regex字符串
- RegEx字符串拆分器