var str = '@username(123) test@username2(234) test'
var newstr = '<a href="localhost/home/userid=123">username</a>test<a href="localhost/home/userid=234">username2</a>test'
我怎样才能在javascript中使用正则表达式
答案 0 :(得分:1)
var str = '@username(123) test@username2(234) test'
document.write(str.replace(/@(\w+?)\((\d+?)\)/g, '<a href="localhost/home/userid=$2">$1</a>'));
// Result: <a href="localhost/home/userid=123">username</a> test<a href="localhost/home/userid=234">username2</a> test