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
Add a ment  | 

3 Answers 3

Reset to default 8

You 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