user188962
user188962

Reputation:

Why isn't this regExp working?

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

Upvotes: 1

Views: 104

Answers (2)

Revolution42
Revolution42

Reputation: 191

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

Upvotes: 0

Gabriele Petrioli
Gabriele Petrioli

Reputation: 195992

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 ..

Upvotes: 4

Related Questions