我试图从*开始使用匹配函数来结束*。我得到一个字符串数组而不是一个字符串。 我的代码是
Str="==Quotes==* We thought that he was going to be -- I shouldn't say this at Christmastime -- but the next messiah.** On [[Barack Obama]]"
regE=/\* \w*/gi
newArr=str.match(regE)
console.log(newArr)
答案 0 :(得分:1)
你的正则表达式略有偏差。要匹配两个星号,您需要查找/\*([^*]*)\*/gi
:
str = "==Quotes==* We thought that he was going to be -- I shouldn't say this at Christmastime -- but the next messiah.** On [[Barack Obama]]";
regE = /\*([^*]*)\*/gi;
newArr = str.match(regE);
console.log(newArr[0]);

请注意, .match()
会返回匹配的数组。为了获得第一个匹配,您只需使用[0]
访问第一个索引,如上所述。
希望这有帮助! :)
答案 1 :(得分:1)
你应该使用:
str = "==Quotes==* We thought that he was going to be -- I shouldn't say this at Christmastime -- but the next messiah.** On [[Barack Obama]]";
regE = /\*.*?\*/gi;
newArr = str.match(regE);
console.log(newArr[0]);
str = "==Quotes==* We thought that he was going to be -- I shouldn't say this at Christmastime -- but the next messiah.** On [[Barack Obama]]";
regE = /\*.*\*/gi;
newArr = str.match(regE);
console.log(newArr[0]);