省略匹配正则表达式结果中的分隔字符



我有这个字符串,我想在javascript:中提取介于^之间但没有^本身的字符串

const input = "^k^ 989 ^s^"
var ptrn = /^(.*?)^/mg;
var results = input.match(ptrn);
console.log(results)  ==> ["^k^" , "^s^]

下面的exec有不同的结果,但我更喜欢匹配方法,而不是exec的循环来获得所有匹配的

let results = ptrn.exec(input);
console.log(results) ==> ["^k^", "k"]

作为另一种方式,您可以使用split来实现这一点。

const input = "^k^ 989 ^s^";
const results = input
.split('^')
.filter((_, index) => index % 2 === 1);
console.log(results);