Bhavin Radadiya
Bhavin Radadiya

Reputation: 63

jquery RegExp function issue

I have to search a string within another string using jQuery RegExp. I have tried following code but its not working.

var sup_spec = ($('#talent_type_sub_list1 li#'+$(this).val()+ '   p').html());          
var quote = 'jazz Dance (Contemporary), Garba (Folk), Ghomar (Folk)';
if (sup_spec.test(quote))
            {
                alert(quote);
            }

Upvotes: 0

Views: 62

Answers (4)

NiGhTTraX
NiGhTTraX

Reputation: 56

String objects don't have a test method, but Regex objects do. To create a Regex object, you use the RegExp constructor:

var quote = new RegExp("jazz Dance");

Or, you can use the Regex literal, like so:

var quote = /jazz Dance/;

Note: don't use quotes around it since that will just create a normal string wrapped in slashes.

To learn about the differences between the two, check this article on MDN.

After you've created a Regex object, you can call its test method and pass in the string you want to search in, like this:

quote.test(sup_spec)

This will return True/False representing whether your needle (the regex) matched the haystack (the string) or not.

As mentioned in other responses, if you don't need the power of Regex, you can just use the indexOf method of String objects, which takes the needle (string you're searching for) and returns the first position where it matches, or -1 if it can't find it.

Upvotes: 1

Jai
Jai

Reputation: 74738

As you mentioned your question doesnot have a javascript regular expression and even you don't need it in your case, you can try this:

var sup_spec = $.trim($('#talent_type_sub_list1 li#'+$(this).val()+ ' p').html());          
var quote = ['jazz Dance (Contemporary)', 'Garba (Folk)', 'Ghomar (Folk)'];
if ($.inArray(sup_spec, quote) != -1){
     alert(quote);
}

make it an array var quote = []; and put all the values the way i suggested then you can check for the element exists or not.

  1. if element exists $.inArray() will return the index of the found element.
  2. if not the it will return -1.
  3. Also you should use $.trim() if you are picking text out of specifi dom node.

A short demo

Upvotes: 0

Kita
Kita

Reputation: 2634

Javascript test() method belongs to RegExp object. I reorganized your code to properly implement RegExp.

/**
 * Escapes regular expression special chars
 * @see http://stackoverflow.com/a/6969486/760211
 */
function escapeRegExp(str) {
  return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
}

var sup_spec = $('#talent_type_sub_list1 li#'+$(this).val()+ ' p').html();
var quote = "jazz Dance (Contemporary), Garba (Folk), Ghomar (Folk)";
var pattern = new RegExp('/' + escapeRegExp(quote) + '/');
if ( pattern.test(sup_spec) ) { alert( quote ); }

Upvotes: 0

Barmar
Barmar

Reputation: 780899

It doesn't look like you need a regexp, since you're looking for a literal string. Use indexOf():

if (sup_spec.indexOf(quote) != -1) {
    alert(quote);
}

Upvotes: 0

Related Questions