繁体   English   中英

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

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

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

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

我需要提取这些字符串-“ string”“需要”“ be”“ tested”我只需要使用RegExp,而无需使用任何其他字符串操作

正如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+)/配合使用. 然后是带有参数1 .slice()单词,以删除. 字符

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)); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM