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

查看:123
本文介绍了正则表达式在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中获取两个字符串之间的字符串

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

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

以下是 RegExplained

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

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