如何在 javascript 中使用正则表达式提取句点 (.) 之后的字符串

How to extract string after period (.) using regular expression in javascript?

本文关键字:句点 之后 字符串 提取 正则表达式 javascript      更新时间:2023-09-26

如果我的字符串是这样的 -

"this is the .string .needed to .be .tested"

我需要提取这些字符串 - "字符串"需要"BE"测试"我只需要使用正则表达式,而不需要任何其他字符串操作

正如

Tushar在评论中所说,你可以使用exec来做这些事情,如下所示:

var regEx = /'.('S+)/g;
var text = "this is the .string .needed to .be .tested";
var words = [];
while (word = regEx.exec(text)) {
   words.push(word[1]);
}
console.log(words);

您可以将.replace()RegExp /'.('w+)/一起使用以匹配.后跟单词,.slice()使用参数1来删除.字符

var str = "this is the .string .needed to .be .tested";
var res = []; 
str.replace(/'.('w+)/g, function(match) {
  res.push(match.slice(1))
});
console.log(res);
您可以使用

match函数

var str = "this is the .string .needed to .be .tested";
var res = str.match(/'.'S+/g).map(e => e.substr(1));
document.write(JSON.stringify(res));