views:

87

answers:

4

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.

A: 

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

The Elite Gentleman
sorry, that's not working
Sarfraz
It selects `'ul.child').slideUp('slow'` that all, should not select `).slideUp(` part
Sarfraz
What happens if you put bracket as I did?
The Elite Gentleman
please see this: http://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.
Sarfraz
A: 

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

RaYell
that's very specific involving the slideup keyword.
Sarfraz
Yes it is, but the question didn't say it should be more generic.
RaYell
please see this: http://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.
Sarfraz
+4  A: 

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>');
Felix Kling
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?
Sarfraz
Basically i want to replace not match.
Sarfraz
@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 Kling
@Felix: please see this: http://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
@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`.
Felix Kling
@Felix: Can you provide your new edit link of jsbin? Thanks
Sarfraz
@Sarfraz: Well it is http://jsbin.com/azovo3/3/edit. But I assume you want to have the stuff inside the quotes red and all the other stuff blue right?
Felix Kling
@Felix: Yes you are right, that's what i was trying to do.
Sarfraz
@Felix: even your edit shows the same problem, whole expression red. It should red only what is inside quotes.
Sarfraz
with your updated regex, it doesn't select other strings. You can check that out by editing in jsbin. Thanks
Sarfraz
@Sarfraz: It is not totally red , the quotes are green and only `slideUp` is red in my case. But you chose the wrong thing for substitution. You should substitute with `<span class="code_string">$1</span>` not `class=comment`. And for some reason there is already a `<span class="code_string">` around it, that is why it is red. I guess this is due to some previous substitution.
Felix Kling
@Sarfraz: I got it. See my updated answer.
Felix Kling
@Felix: +1 Great thanks man for your help :)
Sarfraz
@Sarfraz: Sure thing :) You're welcome!
Felix Kling
@Felix: One last thing though, how about coloring red anything coming in double quotes like you did for single quotes? Thanks
Sarfraz
@Sarfraz: Actually this should be done by the last piece of code in my answer... Just replace your the regex for your single quotes with this. But you have to put the regex for the comments **after** that for the quote otherwise it will be messed up.
Felix Kling
@Felix: I tried that but at some places, it is adding `"comment">` on the output. What could be wrong there? This is the final correction to be made for what I have been trying to do. Thanks
Sarfraz
@Felix: Fixed done, thanks, i moved all comments regex below :)
Sarfraz
A: 
/^[^']*?('[\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?

Rich
The dot `.` does not have to be escaped in character classes.
Felix Kling
please see this: http://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.
Sarfraz
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. http://stackoverflow.com/questions/432493/how-do-you-access-the-matched-groups-in-a-javascript-regex
Rich
@Rich: Thanks for that link. I am not that good at regex, things posted on that link are difficult for me :(
Sarfraz