I have read some of the other tutorials on here about regular expressions, but I am still having trouble creating exactly what I need.
I have an onblur function that does this…
var x = $("#outputpathid").val();
var testwhitespace = new RegExp(/\s/g);
var testdoublequotes = new RegExp(/^\"|\"$/);
if (testwhitespace.test(x) && !testdoublequotes.test(x)) {
$("#outputPathDivWhitespace").dialog({
title: 'Output Path contains whitespace. Click the \'Close\' button to add quotes.',
width: 500,
modal: true,
resizable: false,
buttons: {
'Close': function() {
$("#outputpathid").val('"'+x+'"');
$(this).dialog('close');
}
}
});
}
…I want the function to test whether x, an input field string, contains a whitespace. If it does, also check to see if there are quotes. If there are NOT quotes and it contains a space, then add quotes around the entire string. This works fine until the string has either a beginning or end quote.
I am looking for some type of ‘and’ operator to replace the pipe in the testdoublequotes var. I found that I should be using the ‘?’, but can not get it to work.
Can some please help? If you provide an answer, please explain exactly what you did so I can understand what is going on. Thanks!
Use
.*to match <anything> in between the double quotes..matches any character, and*matches any number of the proceeding whatever. So.*matches any number of any character.The double quotes don’t need to be escaped, by the way. I removed the backslashes.