0

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);
            }
  • 1
    There's no such thing as a jQuery regexp. Regexp is an ordinary Javascript feature, jQuery has nothing specific to them. – Barmar Feb 01 '14 at 10:26
  • Where is the regexp in your code? To create a regexp, you need to call `new RegExp()` or use a RegExp literal surrounded by `//`. – Barmar Feb 01 '14 at 10:27

4 Answers4

1

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.

NiGhTTraX
  • 56
  • 3
0

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);
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

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 ); }
Kita
  • 2,604
  • 19
  • 25
0

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

Community
  • 1
  • 1
Jai
  • 74,255
  • 12
  • 74
  • 103