假设我有一个像$foo$bar$baz $5
我试图通过`$'将字符串拆分为数组,然后删除第一个和第二个元素,然后将数组转换为新字符串。但我想知道是否有一种更优雅的方式呢?
答案 0 :(得分:1)
你可以删除$
的前两个和一些空字符串的文本。
^(\$[^$]+){2}\$ regular expression ^ start of the string \$ search for $ literally [^$] search for any character but not $ + quantifier one or more ( ) group {2} quantifier for exactly two times ( ){2} get the group only two times \$ get the third $
var string = '$foo$bar$baz $5',
result = string.replace(/^(\$[^$]+){2}\$/, '');
console.log(result);

答案 1 :(得分:0)
回顾一下: -
var str = '$foo$bar$baz $5',
delimiter = '$',
start = 3,
tokens = str.split(delimiter).slice(start),
result = tokens.join(delimiter);
console.log(result); //baz $5