【问题标题】:How to use regular expression replacement string [closed]如何使用正则表达式替换字符串 [关闭]
【发布时间】:2013-04-25 07:17:36
【问题描述】:
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 中使用 Regex 来做到这一点

【问题讨论】:

    标签: javascript regex


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 2016-11-27
      • 1970-01-01
      • 1970-01-01
      • 2021-10-07
      • 2015-02-27
      • 1970-01-01
      • 2018-07-13
      相关资源
      最近更新 更多