chromedude
chromedude

Reputation: 4302

How would you change this Javascript regex to exclude "<br/>"?

I have this regex /[^a-z0-9\s]+/ig . I want the string <br/> not to be included in it. How would I edit this to do that?

An example string: var testContent = "Si mi voz muriera en tierra, <br> llevadla al nivel del mar <br> y dejadla en la ribera. <br> <br> Llevadla al nivel del mar <br> y nombradla capitana <br> de un blanco bajel de guerra. <br> <br> Oh mi voz condecorada <br> con la insignia marinera: <br> sobre el corazon un ancla <br> y sobre el ancla una estrella <br> y sobre la estrella el viento <br> y sobre el viento una vela!"

On this string I running this javascript:

testContent.replace(/[^a-z0-9\s]+/ig, 
                  function ($1) { 
                       return ' ' + $1;
                       }
           ).split(" ");

I want this to split at <br/> too instead of at < and br and / and > when it is that string.

Upvotes: 0

Views: 367

Answers (2)

Bohemian
Bohemian

Reputation: 425228

(?!.*<br/>.*$)[^a-z0-9\s]+

It uses a negative look ahead

EDITED: Corrected neg look ahead syntax.

I admit it: This regex does not work. But I'm working on it!

Upvotes: 0

Qtax
Qtax

Reputation: 33918

EDIT Updated for the following problem (what OP means):

so, for.this - you<br>want an array with this: so, ,, for, ., this, -, you, <br>, want

You can use something like:

var array = testContent.match(/[a-z\d]+|<br\s*\/?>|[^a-z\d\s]+/gi);

Will work for both <br> and <br/> ;)

Upvotes: 2

Related Questions