Reputation: 244
Amateur at JavaScript here. I set my self-projects to help myself learn.
I usually find the answers on stackoverflow but several days of googling have not enlightened me on the following.
I’m trying to match whole words in sentences. I plan to do this to wrap the words in <span>
tags.
The regex I’m using is
(\/?+<?+\w+<?+>?+)
seen in use here http://regex101.com/r/nT2yY4
If this code is run on the string this is a <span>string</span>
It returns
<span>
string<
/span>
I'd like it to return
<span>string</span>
while the string <span>two words</span>
returns
<span>two
words</span>
also I'd like punctuation pulled split off as well so "this is a string."
becomes
Upvotes: 0
Views: 750
Reputation: 6761
If you want this is a <span>string</span>
to be split at white space, use:
"this is a <span>string</span>".split(" ");
which gives you:
[ 'this', 'is', 'a', '<span>string</span>' ]
Upvotes: 1
Reputation: 4042
It looks to me like all you care about is the spaces and not the tags, so can't you just use something like:
/([^\W]+)/g
To split on whitespace.
If I test that on "This is a sentence with some words and also multiple words sometimes" then the result is, I think, what you've asked for.
Upvotes: 1