简体   繁体   English

使用正则表达式 js 匹配最后一个最佳单词

[英]Matching the last best word using regex js

I have a regex for matching the text within the brackets.我有一个正则表达式来匹配括号内的文本。 For example the regex https://regex101.com/r/TvweUj/3例如正则表达式https://regex101.com/r/TvweUj/3

/\b(\w)[-'\w]* (?:[-",/\\*&'\w]* ){1,}\(\1[A-Z]{1,}\)/gi

matches MIDI The USB Device Class Definition for MIDI Devices transmits Music Instrument Digital Interface (MIDI).匹配MIDI USB 设备 Class MIDI 设备定义传输乐器数字接口 (MIDI)。 instead of only matching the last 4 words Music Instrument Digital Interface而不是只匹配最后 4 个单词Music Instrument Digital Interface

How do I change my regex to match the recent matching instead from the MIDI The USB Dev *****如何更改我的正则表达式以匹配最近的匹配而不是来自MIDI USB Dev *****

You might use 4 capturing groups with a positive lookahead asserting 4 backreferences to match the uppercase chars between the parenthesis:您可能会使用 4 个捕获组,其中包含肯定的前瞻断言 4 个反向引用来匹配括号之间的大写字符:

\b([A-Z])\w+ ([A-Z])\w+ ([A-Z])\w+ ([A-Z])\w+(?= \(\1\2\3\4\))

Regex demo正则表达式演示

Instead of using \w only, you could use the character classes that you use in the question like [-",/\\*&'\w]*您可以使用在问题中使用的字符类,而不是仅使用\w ,例如[-",/\\*&'\w]*


A more broad pattern could be repeating an uppercase char followed by 1+ word chars \w+ (or use \w* to repeat 0+ word chars) and assert that what follows is only uppercase chars between parenthesis.更广泛的模式可能是重复一个大写字符,后跟 1+ 个单词字符\w+ (或使用\w*重复 0+ 个单词字符)并断言后面的只是括号之间的大写字符。

\b[A-Z]\w+(?: [A-Z]\w+)*(?= \([A-Z]+\))

Regex demo正则表达式演示


If the number of chars are variable that you want to match between the parenthesis and they should match with the number of words before, you could use 2 capturing groups and compare the amount of splitted words with the number of uppercase chars between the parenthesis.如果要在括号之间匹配的字符数是可变的,并且它们应该与之前的单词数匹配,则可以使用 2 个捕获组并将拆分单词的数量与括号之间的大写字符数进行比较。

 let pattern = /\b([AZ][az]*(?: [AZ][az]*)*) \(([AZ]+)\)/; let compare = (ar1, ar2) => ar1.length === ar2.length && ar1.every( (value, index) => value === ar2[index].charAt(0) ); [ "transmits Music Instrument Digital Interface (MIDI).", "transmits Music Instrument Digital Interface (MADI).", "transmits Music Instrument Digital Interface (MID)." ].forEach(s => { let m = s.match(pattern); let res = compare(m[2].split(''), m[1].split(' '))? "Ok -> ": "Not ok -> "; console.log(res + s); })

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

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