admin管理员组文章数量:1406949
I want to match last character of every word in a sentence in which , last character of only those words should be matched whose length is greater than 1.
For example, if sentence is:-
I love regex.
Then regex should match last character of love and regex only, i.e., e and x , not I.
So far i am able to match last character of every word, including those having length 1, using this regex :-
[a-zA-Z0-9](?= |\.|,|$)
But i want to match last character of only those words having length greater than 1. How can i do this?
Test link:- /
I want to match last character of every word in a sentence in which , last character of only those words should be matched whose length is greater than 1.
For example, if sentence is:-
I love regex.
Then regex should match last character of love and regex only, i.e., e and x , not I.
So far i am able to match last character of every word, including those having length 1, using this regex :-
[a-zA-Z0-9](?= |\.|,|$)
But i want to match last character of only those words having length greater than 1. How can i do this?
Test link:- https://regex101./r/7tnXnB/1/
Share asked Jun 14, 2017 at 13:01 Sumit ParakhSumit Parakh 1,1539 silver badges19 bronze badges 2- 1 does it have to be regex? you can split string into array of words and pick last letter of every word by word[word.length-1] – Luke Commented Jun 14, 2017 at 13:08
-
Do you need to account for Unicode letters, like in the word
café
? – Wiktor Stribiżew Commented Jun 14, 2017 at 13:32
3 Answers
Reset to default 8You can use (negated) word boundaries \b
and \B
:
\B\w\b
Here \w
matches a word character, \w\b
asserts a word boundary (therefore it'll only match the last character in a word), and \B
asserts that there is no word boundary before this character.
Try
[A-Z0-9a-z]{2,}
The {2,) makes sure it'll only get characters 2 or more of length
I think this might work for you, although I'm sure there is a shorthanded version of this
[a-zA-Z0-9](?= [a-zA-Z0-9]|\.|,|$)
Edit:
\w(?= \w|\.|,|$)
本文标签: javascriptRegex to match last character of words having length greater than 1Stack Overflow
版权声明:本文标题:javascript - Regex to match last character of words having length greater than 1 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745050999a2639652.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论