[英]Character replacement
I have the following string value. 我有以下字符串值。
var stringVal = [4|4.6]^Size{1}~[6];
I want to replace everything before the first ^
occurrence with [1|5]
how do I go about doing this? 我想用
[1|5]
替换第一个^
出现之前的所有内容,我该怎么做?
Thanks in advance. 提前致谢。
A simple regex will do: 一个简单的正则表达式可以做到:
var stringVal = '[4|4.6]^Size{1}~[6]';
stringVal.replace(/^.*?\^/, '[1|5]^');
#=> "[1|5]^Size{1}~[6]"
Regex explanation: 正则表达式说明:
^ 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
Another, faster way, which would work for this case: 另一种更快的方法,适用于这种情况:
'[1|5]' + stringVal.substr(stringVal.indexOf('^'))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.