Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to check if whitespace present?

Seems pretty simple, but cannot figure out why this javascript code isn't working returning false, when expecting true) - I'm guessing it has got to do something with the escape characters? Could someone please help me write a JS block that will return true if whitespace present?

var inValid = new RegExp("[\s]");
var value = "test space";
var k = inValid.test(value);
alert(k);
like image 371
user1869870 Avatar asked Apr 16 '13 22:04

user1869870


1 Answers

You don't need the brackets, you would need to escape the backslash (if using the string form) and the built-in regex syntax is easier because you don't have to escape backslashes when using the built-in regex syntax.

var inValid = /\s/;
var value = "test space";
var k = inValid.test(value);
alert(k);
like image 83
jfriend00 Avatar answered Oct 24 '22 21:10

jfriend00