the MMM blog

JQuery Selector Wildcard and Regular Expression

JQuery Selector Wildcard and Regular Expression


JQuery selector is similar with CSS selector, but at some point, you might need to use wildcard or regular expression to do more specific selections. The followings are some examples show you how to use them.

string Equals

$("input[name='inputname']")
// example
<input name="inputname">

string Contrains

$("input[name*='inputname']")
// example
<input name="123inputname123">

string Starts with

$("input[name^='inputname']")
// example
<input name="inputname123">

string Ends with

$("input[name$='inputname']")
// example
<input name="123inputname">

string Does not equal to

$("input[name!='inputname']") 
// example
<input name="anything">

string Contains prefix (followed by a hyphen -)

$("input[name|='inputname']")
// example
<input name="inputname-123">

string Contains (separated by a space)

$("input[name~='inputname']")
// example
<input name="inputname anothername">

 

If the above wildcards selector still doesn't meet your needs, you can try the following regular expression selector to select whatever you need.

$('input').filter(function() {
   return this.name.match(/your regular expression here/);
});

 

 

 

"If opportunity doesn't knock, build a door"

Contact me!

Have questions? Feel free to use the form or send me an email with any questions.

I will be responsive to your requests and your questions.