0

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

  1. this
  2. is
  3. a
  4. <span>
  5. string<
  6. /span>

I'd like it to return

  1. this
  2. is
  3. a
  4. <span>string</span>

while the string <span>two words</span> returns

  1. <span>two
  2. words</span>

also I'd like punctuation pulled split off as well so "this is a string." becomes

  1. this
  2. is
  3. a
  4. string
  5. .
2
  • 1
    Someone is going to link to this question: stackoverflow.com/questions/1732348/… Commented Mar 31, 2014 at 8:51
  • What I got from that post was don't use regex with html parsing. Can you offer suggestion of how I should do this in JS? Commented Mar 31, 2014 at 10:12

3 Answers 3

1

Try this regex instead:

/([\w<>\/]+|\.)/g

Description

Regular expression visualization

Demo

http://regex101.com/r/rJ4rR8

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! This gives me a great leg up!
1

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.

1 Comment

I've added clarification to my post regarding punctuation. I want the punctuation split off separate to the words. Sorry. I should have mentioned this.
1

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>' ]

1 Comment

Sorry. I should have put some punctuation in to demonstrate the problem with str.split(" "). "this is a <span>string</span>.".split(" "); I would want the "." to be split off as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.