【问题标题】:javascript replace all next text [closed]javascript替换所有下一个文本[关闭]
【发布时间】:2021-06-04 09:52:41
【问题描述】:

我有这个例子

'test test: field test test 1 - pid 20242004 test test test 543 - pid 20242004 test test test 123 - pid 544745456 test test jdfh 1 - pid 353545 test test test 1 - pid 20242004 test test test 6 - pid 878 field test test 23 - pid 234445'

我想更改单词“pid”旁边的所有文本,因此输出将是:

'test test: field test test 1 - pid <a>20242004</a> test test test 543 - pid <a>20242004</a> test test test 123 - pid <a>544745456</a> test test jdfh 1 - pid <a>353545</a> test test test 1 - pid <a>20242004</a> test test test 6 - pid <a>878</a> field test test 23 - pid <a>234445</a>'

有没有办法用 javascript 做到这一点?

【问题讨论】:

标签: javascript lodash


【解决方案1】:

你可以用正则表达式做到这一点

在这个表达式中,我尝试获取任何包含单词 pid 的字符串,并且在它之后包含大量数字

let str = 'test test: field test test 1 - pid 20242004 test test test 543 - pid 20242004 test test test 123 - pid 544745456 test test jdfh 1 - pid 353545 test test test 1 - pid 20242004 test test test 6 - pid 878 field test test 23 - pid 234445'

let newStr = str.replace(/pid ([\d]+)/g, `pid <a>$1</a>`)

console.log(newStr)

有关replace的更多信息,请查看MDN

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-30
    • 2017-06-29
    • 1970-01-01
    • 1970-01-01
    • 2016-02-29
    • 2021-06-16
    • 1970-01-01
    相关资源
    最近更新 更多