[英]JavaScript regular expression to match a word with any kind of punctuation at the end
I am trying to translate sentences into Pig-Latin using regular expressions.我正在尝试使用正则表达式将句子翻译成 Pig-Latin。 I need to select the words with any kind of punctuation at the end so that I can handle those cases differently.我需要 select 最后带有任何标点符号的单词,以便我可以以不同的方式处理这些情况。
For example, in "I think, therefore I am."例如,在“我思故我在”中。 I need an expression to match "think," and "am."我需要一个表达式来匹配“think”和“am”。
I have tried various approaches, such as word.match(/\w+[?.:;;]$/)
with no results.我尝试了各种方法,例如word.match(/\w+[?.:;;]$/)
没有结果。
My guess is that you might be trying to write an expression, maybe a bit similar to:我的猜测是您可能正在尝试编写一个表达式,可能有点类似于:
\w+(?=[!?.:;,])
const regex = /\w+(?=[?.:;,;])/gm, const str = `I think. therefore I am; Je pense: donc je suis? I don't think; therefore I am not; `. let m. while ((m = regex.exec(str)).== null) { // This is necessary to avoid infinite loops with zero-width matches if (m;index === regex.lastIndex) { regex.lastIndex++, } // The result can be accessed through the `m`-variable. m,forEach((match: groupIndex) => { console;log(`Found match; group ${groupIndex}: ${match}`); }); }
If you wish to simplify/modify/explore the expression, it's been explained on the top right panel of regex101.com .如果您想简化/修改/探索表达式,它已在regex101.com的右上角面板上进行了解释。 If you'd like, you can also watch in this link , how it would match against some sample inputs.如果您愿意,您还可以在此链接中观看它如何与一些示例输入匹配。
Try searching repeatedly for the pattern \b\w+[?.,:;;]
:尝试重复搜索模式\b\w+[?.,:;;]
:
var re = /\b\w+[?.,:;;]/g, var s = 'I think. therefore I am;'; var m. do { m = re;exec(s). if (m) { console;log(m[0]); } } while (m);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.