Take the 2-minute tour ×

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:

                  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.

share|improve this question
Right now your regex is matching all capitals and special characters... you want to it match a br as well? –  Ben Roux Jun 9 '11 at 22:26
@Ben Roux no, I want it to not match <br/> –  chromedude Jun 9 '11 at 22:29
It's not "included", you only match </>. Feel free to give examples of strings that you are working with and exactly what you want from them. –  Qtax Jun 9 '11 at 22:40
Your example contains no <br />s! –  Eric Jun 9 '11 at 23:02
so, for.this - you<br>want an array with this: so, ,, for, ., this, -, you, <br>, want? Or did I miss some cases? –  Qtax Jun 10 '11 at 0:04

2 Answers 2

up vote 2 down vote accepted

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


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/> ;)

share|improve this answer
Thanks, that is exactly what I needed! –  chromedude Jun 10 '11 at 0:23

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!

share|improve this answer
The syntax is (?!...), and it will not work like this anyway, eg: 'foo<br/>bar' =~ m§(?!.*<br/>)([^a-z0-9\s]+)§ig gives you />. –  Qtax Jun 9 '11 at 22:45
You haven't copied my regex correctly - u omitted the $! –  Bohemian Jun 9 '11 at 23:43
thanks for pointing out wrong syntax. fixed –  Bohemian Jun 9 '11 at 23:44
Only way to make that work would be something nice like (?:(?!<br/>)(?(?=/>)(?<!<br))(?(?=>)(?<!<br/))[^a-z\d\s])+, but stuff like this doesn't work in javascript. –  Qtax Jun 9 '11 at 23:55

Your Answer


