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.

Sarfraz
  • 377,238
  • 77
  • 533
  • 578
  • Do you try to parse JavaScript code with regular expressions? – Gumbo Apr 09 '10 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. – Sarfraz Apr 09 '10 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. – Sarfraz Apr 09 '10 at 13:47
  • Give the question a meaningful title – Shermozle Apr 14 '10 at 06:06
  • @Shermozle: You are perfectionist, what should be the title and would you consider your voting if i change the title? – Sarfraz Apr 14 '10 at 06:18

4 Answers4

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>');
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • I want to use regex something like this: `the_code = the_code.replace(/^[^']*?('[\w.]*')*$/,'$1');`. How do i modify my code as per your regex? – Sarfraz Apr 09 '10 at 13:48
  • Basically i want to replace not match. – Sarfraz Apr 09 '10 at 13:49
  • @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 Apr 09 '10 at 13:58
  • @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 Apr 09 '10 at 14:00
  • @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 Apr 09 '10 at 14:12
  • @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 Apr 09 '10 at 14:21
  • @Felix: even your edit shows the same problem, whole expression red. It should red only what is inside quotes. – Sarfraz Apr 09 '10 at 14:27
  • with your updated regex, it doesn't select other strings. You can check that out by editing in jsbin. Thanks – Sarfraz Apr 09 '10 at 14:32
  • @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 `$1` not `class=comment`. And for some reason there is already a `` around it, that is why it is red. I guess this is due to some previous substitution. – Felix Kling Apr 09 '10 at 14:33
  • @Felix: One last thing though, how about coloring red anything coming in double quotes like you did for single quotes? Thanks – Sarfraz Apr 09 '10 at 17:48
  • @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 Apr 09 '10 at 17:52
  • @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 Apr 09 '10 at 18:22
  • @Felix: Fixed done, thanks, i moved all comments regex below :) – Sarfraz Apr 09 '10 at 18:29
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

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
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

RaYell
  • 69,610
  • 20
  • 126
  • 152
  • that's very specific involving the slideup keyword. – Sarfraz Apr 09 '10 at 13:51
  • Yes it is, but the question didn't say it should be more generic. – RaYell Apr 09 '10 at 13:56
  • 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 Apr 09 '10 at 14:05
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?

Rich
  • 36,270
  • 31
  • 115
  • 154
  • The dot `.` does not have to be escaped in character classes. – Felix Kling Apr 09 '10 at 13:42
  • 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 Apr 09 '10 at 14:06
  • 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 Apr 09 '10 at 14:14
  • Thanks for that link. I am not that good at regex, things posted on that link are difficult for me :( – Sarfraz Apr 09 '10 at 14:19