-6
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中使用正则表达式

4

1 回答 1

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
于 2013-04-25T07:26:35.370 回答