正则表达式逃避括号 [英] Regex to escape the parentheses

查看:97
本文介绍了正则表达式逃避括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了在JavaScript中使用正则表达式来逃避括号的不同方法,但我仍然无法使其工作。

I tried different ways to escape the parentheses using regex in JavaScript but I still can't make it work.

这是字符串:

"abc(blah (blah) blah()...).def(blah() (blah).. () ...)"

我想要检测到这一点:

abc().def() 

使用此代码,它返回false。

Using this code, it returns false.

 str.match(/abc\([^)]*\)\.def\([^)]*\)/i);

你能告诉我为什么我的正则表达式无效吗?

Can you please tell me why my regex is not working?

推荐答案

此正则表达式将匹配您提供的字符串:

This regex will match the string you provided:

(abc\().+(\)\.def\().+(\))

使用反向引用 $ 1 $ 2 $ 3 将产生 abc()。def()

如果你不想要反向引用,只需使用它:

Or just use this if you don't want the back references:

abc\(.+\)\.def\(.+\)

这篇关于正则表达式逃避括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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