Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript RegEx quote

I'm having trouble getting this Javascript Regular Expression to work. What i want is to find a character that starts with @" has any characters in the middle and ends with ". I will also need it in a single quote from.

The tricky part for me, is that it cant be starting with @" and ending with " because the string it's looking through could look like [UIImage imageNamed:@"glass-round-red-green-button.png"].

So far what i have is this.

regex: new RegExp('\\@"?\\w+\\b"*', 'g')

like image 924
cnotethegr8 Avatar asked Jun 29 '11 12:06

cnotethegr8


1 Answers

Try this regular expression:

/@(["'])[^]*?\1/g

An explanation:

  • @(["']) matches either @" or @'
  • [^]*? matches any arbitrary character ([^] contains all characters in opposite to . that doesn’t contain line-breaks), but in a non-greedy manner
  • \1 matches the same character as matches with (["'])

Using the literal RegExp syntax /…/ is more convenient. Note that this doesn’t escape sequences like \" into account.

like image 175
Gumbo Avatar answered Sep 30 '22 13:09

Gumbo