Hello,
Suppose I have this:
$('ul.child').slideUp('slow');
What would be the regex to find 'ul.child'
and 'slow'
(including quotes) in the above expression.
Hello,
Suppose I have this:
$('ul.child').slideUp('slow');
What would be the regex to find 'ul.child'
and 'slow'
(including quotes) in the above expression.
Try
/('[^']*?')/
e.g.
var test = "$('ul.child').slideUp('slow');";
matches = test.match(/('[^']*?')/g);
That's the closest I could get in a hurry. Hope this helps. When I come back, I'll tackle this
Don't know why you possibly will need that but that will do the trick.
var str = "$('ul.child').slideUp('slow');";
var matches = str.match(/\$\((.+)\)\.slideUp\((.+)\);/);
console.log(matches[1]); // 'ul.child' (with quotes)
console.log(matches[2]); // 'slow' (with quotes)
And yes, matches
indices are correct, matches[0]
contains the whole matched part, while indices >= 1 contain matched groups
This should do it:
var a = "$('ul.child').slideUp('slow');";
var matches = a.match(/'[\w.]*'/g));
// matches[0] = 'ul.child'
// matches[1] = 'slow'
g
is a modifier and matches every occurrence of the expression.
If you want to match more expressions, like ul li
, ul + li
or ul, li
, you have to put those additional characters into the character class.
Update 1 was not helping.
Update2:
You had a slight mistake in one of your regex. Change this:
// single quote string
the_code = the_code.replace(/('.+')/g,'<span class="code_string">$1</span>');
to
// single quote string
the_code = the_code.replace(/('.+?')/g,'<span class="code_string">$1</span>')
You have to make it non-greedy (with the ?
) in order to not match to the last occurrence of a '
, but to the next one.
See here: http://jsbin.com/azovo3/4
If you want to match single and double quote, chage it to this:
the_code = the_code.replace(/(('|").+?("|'))/g,'<span class="code_string">$1</span>');
/^[^']*?('[\w\.]*')*$/
I did this on the following regex tester page and it found both of your strings:
You can use capture groups to get the matches easily. The expression goes:
[^']*?
('[\w\.]*')*
Is that close to what you want?