Reputation: 23
I have string, something like that:
You're so beaulifull! Don't say me no!
if i try to match all words by regexp, using \w+
i'll get output like that:
You
re
so
beaulifull
Don
t
say
me
no
but what regex should i use to match words with apostrophe, so output will be correct?
Upvotes: 2
Views: 3847
Reputation: 627087
You can use
\w+(?:'\w+)*
\b\p{L}+(?:'\p{L}+)*\b
See regex #1 and regex #2 demo.
Details:
\b
- a word boundary\p{L}+
- one or more letters(?:'\p{L}+)*
- zero or more sequences of a '
char followed with one or more letters\b
- a word boundaryUpvotes: 0
Reputation: 4694
Regex: /\w+\'\w+/g
To verify this quick, we can go to the browser console and check the following:
var str = "You're so beaulifull! Don't say me no!";
str.match(/\w+\'\w+/g);
gives us: ["You're", "Don't"]
Upvotes: 1
Reputation: 593
If you don't mind a word starting or ending with an apostrophe [\w']+
will suffice.
However; if you don't want neither of those you could try something like this: \w+('\w+)?
Upvotes: 0
Reputation: 2425
Try creating a character class including both \w
and an apostrophe literal, so something like this: [\w']+
Upvotes: 2