如何使用正则表达式(JS)替换如下所示的长字符串中的字符串?

我的字符串可能是以下之一:


str = "this may have bgskin-akb4c hello how";
or
str = "have bgskin-a2b3c hello";
or
str = "bgskin-xyz2 hello";
or
str = "bgskin-bbc";


在上面的字符串中,我想用新的字符串值替换以“ bgskin”开头的单词的第二部分(例如:-akb4c,-xyz2),例如:“ have bgskin-a2b3c hello”变为“ have bgskin-newstr你好”

感谢您的帮助。

非常感谢,
大号

最佳答案

用JavaScript

str.replace(/bgskin-\w+/g, 'bgskin-newstr');

关于javascript - 替换一组字符串中的一个字符串-正则表达式,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5939682/

10-10 14:19