user961627
user961627

Reputation: 12747

jquery simple regex search for substring included

I'm trying out a very simple regex, and my question is that I just need to make sure that if is one of the following terms is found in the data-title of my div, then the alert says so: Person Name, Email, Title and a few more strings.

I've used a few combinations but I haven't been able to do it... so I've made a fiddle here: jsfiddle, I've just removed my regex attempts apart from the |, so I've left only the main phrases I want to catch. Could someone please help? I know it could be done using substring or indexOf but I want to do it with regex.

JS:

title = $('#me').attr('data-title');
alert(title);

if (title.match((Person Name) | (Email) | (Message) | (Company) | (Mobile) | (Title))) alert("It matched!");​

HTML:

<div id='me' data-title='en_Person name'>MY NAME IS FOO</div>

Upvotes: 2

Views: 321

Answers (2)

Explosion Pills
Explosion Pills

Reputation: 191749

http://jsfiddle.net/aLdWD/2/

You need to wrap regular expressions in the / delimeter; what you had was just a syntax error. Also you need to use the i modifier to ignore case (you had "Person name" in the title but "Person Name" in the expression). The parentheses don't add anything.

Upvotes: 1

Blazemonger
Blazemonger

Reputation: 92903

Try using a proper regex in your argument to String.match:

if (title.match(/Person Name|Email|Message|Company|Mobile|Title/)) {
    alert("It matched!");
}​

http://jsfiddle.net/mblase75/aLdWD/3/

Upvotes: 1

Related Questions