在Javascript中获取括号之间的字符串的正则表达式 [英] Regular Expression to get a string between parentheses in Javascript

查看:27
本文介绍了在Javascript中获取括号之间的字符串的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个正则表达式,它返回一个位于括号之间的字符串.例如:我想获取位于字符串("和)"之间的字符串

I am trying to write a regular expression which returns a string which is between parentheses. For example: I want to get the string which resides between the strings "(" and ")"

I expect five hundred dollars ($500).

会回来

$500

找到 正则表达式获取字符串Javascript 中两个字符串之间

但我是正则表达式的新手.我不知道如何在正则表达式中使用 '(', ')'

But I'm new with regex. I don't know how to use '(', ')' in regexp

推荐答案

您需要创建一组转义的(带有 )括号(匹配括号)和一组常规括号创建您的捕获组:

You need to create a set of escaped (with ) parentheses (that match the parentheses) and a group of regular parentheses that create your capturing group:

var regExp = /(([^)]+))/;
var matches = regExp.exec("I expect five hundred dollars ($500).");

//matches[1] contains the value between the parentheses
console.log(matches[1]);

细分:

  • ( : 匹配一个左括号
  • ( : 开始捕获组
  • [^)]+:匹配一个或多个非)字符
  • ) : 结束捕获组
  • ) : 匹配右括号
  • ( : match an opening parentheses
  • ( : begin capturing group
  • [^)]+: match one or more non ) characters
  • ) : end capturing group
  • ) : match closing parentheses

这是对 正则解释

这篇关于在Javascript中获取括号之间的字符串的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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