[英]How to extract string after period (.) using regular expression in javascript?
if my string is like this - 如果我的字符串是这样的-
"this is the .string .needed to .be .tested"
i need to extract these strings - "string" "needed" "be" "tested" I need only using RegExp but not any other string operations 我需要提取这些字符串-“ string”“需要”“ be”“ tested”我只需要使用RegExp,而无需使用任何其他字符串操作
as Tushar said in the comment, you can use exec
to do this stuff, something like the following: 正如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);
You can use .replace()
with RegExp
/\\.(\\w+)/
to match .
您可以将.replace()
与RegExp
/\\.(\\w+)/
配合使用.
followed by word, .slice()
with parameter 1
to remove .
然后是带有参数1
.slice()
单词,以删除.
character 字符
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);
You can use match
function 您可以使用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.