Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
570 views
in Technique[技术] by (71.8m points)

javascript - Regex Match a character which is not followed by another specific character

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.

e.g.

Match < of all the html tags

<body>

And ignore html tags which are followed by <%

<% if %> 

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.

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

1. Searching for < followed by anything but the % character, then ignoring it

(<)(?:[^%])

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

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

2. (Better, if supported) Searching for input not followed by % with a negative lookahead

<(?!%)

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

If you also want to do it for %>, you can just "reverse" the first option :

(?:[^%])(>)

Or you need a negative lookbehind :

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

(?<!%)>


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...