I’m testing if input class="k" contains valid characters for the HTML ID attribute, showing an alert if not:
ID and NAME tokens must begin with a letter ([A-Za-z]) and may be
followed by any number of letters, digits ([0-9]), hyphens (“-“),
underscores (“_”), colons (“:”), and periods (“.”).
I’d like to combine both regular expression in logical OR. Is there a way to combine them in a single regex?
$('.k').keyup(function(){
if((/^[\d]/).test($(this).val())) alert('Not valid!');
if((/[\s]/).test($(this).val())) alert('Not valid!');
});
The pipe is the logical OR in a RegEx. In this case, parens are not necessary. However, usually, you want to group the subpatterns:
The pattern for ID (
\w=[a-zA-Z0-9_]):