RegEx - 仅在未执行或后跟相同字符时匹配字符 [英] RegEx - Match Character only when it's not proceeded or followed by same character

查看:71
本文介绍了RegEx - 仅在未执行或后跟相同字符时匹配字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何匹配下面字符串中text周围的引号,而不是使用RegEx匹配TEST TEXT周围的引号。我只想在他们自己的时候引用它。我尝试了一个负向前瞻(第二个引用)但它仍然捕获了TEST TEXT周围两个引号中的第二个。

How would I match the quotations around "text" in the string below and not around "TEST TEXT" using RegEx. I wanted just quotations only when they are by themselves. I tried a negative lookahead (for a second quote) but it still captured the second of the two quotes around TEST TEXT.

This is some "text". This is also some ""TEST TEXT""

请注意我需要这个来扩展,所以有时它就在这样的字符串中间:

Be aware that I need this to scale so sometimes it would be right in the middle of a string so something like this:

/(\s|\w)(\")(?!")/g (using $2...)

可以在此工作示例但不是如果字符串是:

Would work in this example but not if the string was:

This is some^"text".This is also some ""TEST TEXT""

我只需要自己做引号。

仅供参考,这需要是Javascript RegEx,所以lookbehind对我来说不适合这个。

FYI, this needs to be Javascript RegEx so lookbehind would not be an option for me for this one.

推荐答案

我不确定我是否真的了解你的需求。我会发布这个答案来检查它是否对你有所帮助,但如果没有,我可以将其删除。

I'm not sure if I really understood well your needs. I'll post this answer to check if it helps you but I can delete it if it doesn't.

那么,这是你想要的这个正则表达式:

So, is this what you want using this regex:

"\w+?"

工作演示

Working demo

顺便说一下,如果你只想获得<$ c $内的内容c>...你可以使用这个正则表达式:

By the way, if you just want to get the content within "..." you can use this regex:

"(\w+?)"

工作演示

Working demo

这篇关于RegEx - 仅在未执行或后跟相同字符时匹配字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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