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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With