简体   繁体   English

如何使用 JavaScript 正则表达式提取字符串中的最后一个单词?

[英]How to extract the last word in a string with a JavaScript regex?

I need is the last match.我需要的是最后一场比赛。 In the case below the word test without the $ signs or any other special character:在没有$符号或任何其他特殊字符的单词test下面的情况下:

Test String:测试字符串:

$this$ $is$ $a$ $test$ 

Regex:正则表达式:

\b(\w+)\b

The $ represents the end of the string, so... $代表字符串的结尾,所以...

\b(\w+)$

However, your test string seems to have dollar sign delimiters, so if those are always there, then you can use that instead of \\b .但是,您的测试字符串似乎有美元符号分隔符,因此如果这些分隔符始终存在,那么您可以使用它代替\\b

\$(\w+)\$$

 var s = "$this$ $is$ $a$ $test$"; document.body.textContent = /\\$(\\w+)\\$$/.exec(s)[1];

If there could be trailing spaces, then add \\s* before the end.如果可能有尾随空格,则在末尾添加\\s*

\$(\w+)\$\s*$

And finally, if there could be other non-word stuff at the end, then use \\W* instead.最后,如果最后可能还有其他非单词内容,则使用\\W*代替。

\b(\w+)\W*$

In some cases a word may be proceeded by non-word characters, for example, take the following sentence:在某些情况下,一个单词可能由非单词字符进行,例如,取以下句子:

Marvelous Marvin Hagler was a very talented boxer!

If we want to match the word boxer all previous answers will not suffice due the fact we have an exclamation mark character proceeding the word.如果我们想匹配boxer这个词,之前的所有答案都不够,因为我们在这个词前面有一个感叹号字符。 In order for us to ensure a successful capture the following expression will suffice and in addition take into account extraneous whitespace, newlines and any non-word character.为了让我们确保成功捕获,以下表达式就足够了,此外还考虑了无关的空格、换行符和任何非单词字符。

[a-zA-Z]+?(?=\s*?[^\w]*?$)

https://regex101.com/r/D3bRHW/1 https://regex101.com/r/D3bRHW/1

We are informing upon the following:我们通知以下内容:

  1. We are looking for letters only, either uppercase or lowercase.我们只寻找字母,大写或小写。
  2. We will expand only as necessary.我们只会在必要时进行扩展。
  3. We leverage a positive lookahead.我们利用积极的前瞻性。
  4. We exclude any word boundary.我们排除任何单词边界。
  5. We expand that exclusion,我们扩大了排除范围,
  6. We assert end of line.我们断言行尾。

The benefit here are that we do not need to assert any flags or word boundaries, it will take into account non-word characters and we do not need to reach for negate.这里的好处是我们不需要断言任何标志或单词边界,它将考虑非单词字符并且我们不需要达到否定。

var input = "$this$ $is$ $a$ $test$";

如果你使用var result = input.match("\\b(\\w+)\\b")接下来将返回所有匹配的数组,你可以通过对结果使用pop()或通过执行以下操作来获取它: result[result.length]

Your regex will find a word, and since regexes operate left to right it will find the first word.您的正则表达式会找到一个单词,并且由于正则表达式从左到右运行,它将找到第一个单词。

A \\w+ matches as many consecutive alphanumeric character as it can, but it must match at least 1. \\w+匹配尽可能多的连续字母数字字符,但它必须至少匹配 1 个。
A \\b matches an alphanumeric character next to a non-alphanumeric character. \\b匹配非字母数字字符旁边的字母数字字符。 In your case this matches the '$' characters.在您的情况下,这与'$'字符匹配。

What you need is to anchor your regex to the end of the input which is denoted in a regex by the $ character.您需要的是将您的正则表达式锚定到输入的末尾,该输入在正则表达式中由$字符表示。

To support an input that may have more than just a '$' character at the end of the line, spaces or a period for instance, you can use \\W+ which matches as many non-alphanumeric characters as it can:例如,为了支持在行尾可能有多个'$'字符、空格或句点的输入,您可以使用\\W+匹配尽可能多的非字母数字字符:

\$(\w+)\W+$

Avoid regex - use .split and .pop the result.避免使用正则表达式 - 使用.split.pop结果。 Use .replace to remove the special characters:使用.replace删除特殊字符:

var match = str.split(' ').pop().replace(/[^\w\s]/gi, '');

DEMO演示

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

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