我有以下字符串值。
var stringVal = [4|4.6]^Size{1}~[6];
我想用 [1|5]
替换第一个 ^
出现之前的所有内容,我该如何执行此操作?
提前致谢。
最佳答案
一个简单的正则表达式就可以了:
var stringVal = '[4|4.6]^Size{1}~[6]';
stringVal.replace(/^.*?\^/, '[1|5]^');
#=> "[1|5]^Size{1}~[6]"
正则表达式说明:
^ start of string
. any character
*? repeat >= 0 times, but match as less characters as possible (non-greedy)
\^ match '^' (a simple `^` matches the start of the string, so we need to escape it
另一种更快的方法,适用于这种情况:
'[1|5]' + stringVal.substr(stringVal.indexOf('^'))
关于javascript - 角色替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16438691/