0

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.

5
  • Do you try to parse JavaScript code with regular expressions? Commented Apr 9, 2010 at 13:33
  • @Gumbo: yes but here I think it is not about any language, it is about finding what is in-between the single quotes. Commented Apr 9, 2010 at 13:35
  • Guys your suggested regex selects 'ul.child').slideUp('slow' that all, should not select ).slideUp( part. It should only select what is in-between the single quotes. Commented Apr 9, 2010 at 13:47
  • Give the question a meaningful title Commented Apr 14, 2010 at 6:06
  • @Shermozle: You are perfectionist, what should be the title and would you consider your voting if i change the title? Commented Apr 14, 2010 at 6:18

4 Answers 4

3

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>');
Sign up to request clarification or add additional context in comments.

13 Comments

I want to use regex something like this: the_code = the_code.replace(/^[^']*?('[\w.]*')*$/,'<span class="comment">$1</span>');. How do i modify my code as per your regex?
Basically i want to replace not match.
@Sarfraz: It would be very helpful if you show an example of with which strings you start from and what do you want to have as result.
@Felix: please see this: jsbin.com/azovo3/edit . You can edit your part where your name is written. If you preview that, you would see the problem at the line $('ul.child').slideUp('slow');, it makes whole expression red.
@Sarfraz: I edit it, I don't know whether it looks right, but the HTML is how it should be like ;) But I noticed that $( is outside of the code string span.
|
0

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

3 Comments

that's very specific involving the slideup keyword.
Yes it is, but the question didn't say it should be more generic.
please see this: jsbin.com/azovo3/edit . If you preview that, you would see the problem at the line $('ul.child').slideUp('slow');, it makes whole expression red.
0
/^[^']*?('[\w\.]*')*$/

I did this on the following regex tester page and it found both of your strings:

RegexPal

You can use capture groups to get the matches easily. The expression goes:

  1. Anything not a single quote, lazy match all [^']*?
  2. Capture anything starting with a quote, containing any number of word characters or periods, followed by a single quote greedy match all ('[\w\.]*')*

Is that close to what you want?

4 Comments

The dot . does not have to be escaped in character classes.
please see this: jsbin.com/azovo3/edit . If you preview that, you would see the problem at the line $('ul.child').slideUp('slow');, it makes whole expression red.
yeah, you gotta pull the capture group out. check out the answer to this question. he runs regex.exec and a matches array gets returned. you pull the matches out by position and test the size, etc to get at them. stackoverflow.com/questions/432493/…
Thanks for that link. I am not that good at regex, things posted on that link are difficult for me :(
0

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

3 Comments

It selects 'ul.child').slideUp('slow' that all, should not select ).slideUp( part
What happens if you put bracket as I did?
please see this: jsbin.com/azovo3/edit . If you preview that, you would see the problem at the line $('ul.child').slideUp('slow');, it makes whole expression red.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.