简体   繁体   English

JavaScript 正则表达式匹配结尾带有任何标点符号的单词

[英]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+(?=[!?.:;,])

Demo演示

 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.如果您愿意,您还可以在此链接中观看它如何与一些示例输入匹配。


RegEx Circuit正则表达式电路

jex.im visualizes regular expressions: jex.im可视化正则表达式:

在此处输入图像描述

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.

相关问题 javascript正则表达式不匹配单词 - javascript regular expression to not match a word .NET 正则表达式匹配来自任何语言的任何类型的字母 - .NET Regular Expression to match any kind of letter from any language 标点符号的Javascript正则表达式(国际)? - Javascript regular expression for punctuation (international)? javascript正则表达式匹配单词,后跟数字 - javascript regular expression to match a word and followed by digits Javascript正则表达式:以任意顺序匹配2个子字符串 - Javascript regular expression : match 2 substrings in any order Javascript正则表达式:匹配除允许的特殊字符外的所有非单词 - Javascript regular expression: match any not-word except allowed special characters 在行尾匹配单词和标点之间的所有内容 - Match everything between word and punctuation at end of line JS中的正则表达式 - 匹配除了最后的可选s之外的整个单词 - Regular Expression in JS - Match whole word except for optional s at the end 正则表达式在开始时将任何字符与特定字符串匹配 - Regular expression match any character at the beginning end with specific string 是否存在类似于Vim正则表达式的单词原子“\\ &lt;”和“\\&gt;”的开头和结尾的JavaScript等价物? - Are there JavaScript equivalents of the Vim regular expression start and end of word atoms “\<” and “\>”?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM