正则表达式匹配未跟随另一个特定字符的字符 [英] Regex Match a character which is not followed by another specific character

查看:175
本文介绍了正则表达式匹配未跟随另一个特定字符的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Brackets编写CodeMirror扩展。要defineSimpleCodeMode,我需要做一些模式匹配,我想弄清楚如何实现$ subject。

I'm writing a CodeMirror extension for Brackets. To defineSimpleCodeMode I need to do some pattern matching and I'm trying to figure out how to achieve $subject.

例如

匹配所有html标签的<

Match < of all the html tags

<body>

并忽略后跟<%

<% if %> 

注意:我只想获得起始< of it

Note: I only want to get the starting < of it

如果有人可以帮助我,那将是一个很大的帮助。如果您需要更多详细信息,请告诉我。

If some can help me out it would be a great help. Please do let me know if you need anymore details.

谢谢!

推荐答案

虽然这似乎是一个坏主意,但我可以看到两种方法:

While this seems to be a bad idea, I can see two ways of doing it :

(<)(?:[^%])

[^] sequence允许您搜索除以下字符之外的任何内容。

The [^] sequence allows you to search for anything but the following character.

(?:)序列适用于非捕获组。

The (?:) sequence is for non capturing groups.

<(?!%)

(?!)序列如果与以下字符不匹配,则成功,但不是捕获。

The (?!) sequence succeeds if it doesn't match the following character, but is not captured.

(?:[^%])(>)

(小心这里,前瞻不会起作用,因为你需要向后看)

(careful here, the lookahead won't work as you need to go backwards)

(?<!%)>

这篇关于正则表达式匹配未跟随另一个特定字符的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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