Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/439.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript替换所有下一个文本_Javascript_Lodash - Fatal编程技术网

javascript替换所有下一个文本

javascript替换所有下一个文本,javascript,lodash,Javascript,Lodash,我以这篇文章为例 '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' 我想更改

我以这篇文章为例

'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>'
测试测试:现场测试测试1-pid 20242004测试测试543-pid 20242004测试测试123-pid 544745456测试jdfh 1-pid 353545测试测试1-pid 20242004测试测试6-pid 878现场测试23-pid 234445'
有没有办法用javascript做到这一点?

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

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

let str='测试测试:现场测试测试1-pid 20242004测试543-pid 20242004测试123-pid 5447456测试jdfh 1-pid 353545测试测试1-pid 20242004测试6-pid 878现场测试23-pid 234445'
让newStr=str.replace(/pid([\d]+)/g,`pid$1`)

console.log(newStr)
是的,这在JavaScription中是可能的。您可能需要查看并尝试自己解决此问题,然后在需要进一步帮助时返回(请参阅)