Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate Regex with minimum length and new line

Tags:

javascript

I am trying to match string having length > 10

var value = "Lorem Ipsum is simply dummy text of the printing and type";
/^.{10,}$/.test(value);

returns true;

But, if I have a string with new line character then it fails.

How can i update regular expression to fix that.

I know I can just check .length > 10 or replace new line with space in value. But, i want to update regular expression.

like image 920
Yogesh Avatar asked Jan 19 '26 17:01

Yogesh


1 Answers

JavaScript does not have a native option for dot matches newlines. To get around this, use a different selector:

[\S\s]

This will match any Whitespace or Non-Whitespace character.

var s = "some\ntext\n",
    r = /^[\S\s]{10,}$/;
console.log(r.test(s));

And, the obligatory fiddle: http://jsfiddle.net/kND83/

There are libraries, such as http://xregexp.com/, that add options for dot matches newlines, but all they do is sub in [\S\s] for the . in your Regex.

like image 178
pete Avatar answered Jan 22 '26 05:01

pete