views:

68

answers:

2

I have this to validate a textarea.

Here is the exp:

  var desExp = /^\s*(\w[^\w]*){3}.*$/;

This works fine when typing on one line something like "really nice car".

But when typing into several lines like this:

Got receipt. Brand new! // new line here
Shipping included. // new line here
0704-256568

I think the error comes up because it doesn't like 'enters' or 'new lines'. If so, this must be included in the regexp!

This gives an error because it DOESN'T match the expression. Could anybody tell me why it doesn't match?

Thanks

+5  A: 

make it var desExp = /^\s*(\w[^\w]*){3}.*$/gm;

Notice the g and the m options at the end which makes the regex global and multiline ..

Gaby
A: 

Newline regex isnt supported across all the browsers.

Depending on your target browsers you can either add multiline mode (not supported everywhere)

/^\s*(\w[^\w]*){3}.*$/m

The other option is to replace new lines with a unique string, run the regex then replace the unique strings backs

str = str.replace(/\n/g,'xxxStringxxx')
// Do regex
str = replace(/xxxStringxxx/g,'\n');
Revolution42
Interesting. What browsers don't support multiline mode?
Max Shawabkeh
@Max S: Probably none worth worrying about.
Andy E