开发者

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


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜