Javascript RegEx报价 [英] Javascript RegEx quote

查看:87
本文介绍了Javascript RegEx报价的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使这个Javascript正则表达式工作。
我想要的是找到一个以开头的字符@中间有任何字符,以结尾。我还需要一个单引号。

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.

对我来说,棘手的部分是它不能以 @并以结尾,因为它正在查看的字符串看起来像 [UIImage imageNamed:@glass-round-red-green- button.png]

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.

正则表达式:新的RegExp('\\ @?\\w + \\b*','g')

推荐答案

试试这个正则表达式:

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

解释:


  • @(['])匹配 @ @'

  • [^] *?匹配任意字符( [^] 包含与相反的所有字符。不包含行-breaks),但是以非贪婪的方式

  • \1 匹配相同的字符匹配(['])

  • @(["']) 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 (["'])

使用文字RegExp语法 / ... / 更方便。请注意,这不会将 \等序列转义为帐号。

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

这篇关于Javascript RegEx报价的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆