简体   繁体   中英

How to match a sequence of characters not preceded by specific character?

I have a series of numbers in my string, and I want to select them with RegEx. Though, if a certain sequence of numbers are preceded by ' @ ' character(without quotations), then it should skip it. I do not know how should I solve it in Javascript.

I know I should use Lookahead to check the condition: (?!@[az]) But the above rule is used only to NOT select the numbers preceded by @, what about selecting those which are not preceded by @ ? I need to say to RegEx to skip @\\d and rather select only (?!@\\d) . How should I do this?

For instance I have this string:

helloMrRobert5555AndGoodByeMrSteve@8888

Expected output:

5555

I think you can use a simple regex like this (if the length of number is fixed):

/[^@](\d{4})/g

And use substitution $1 .

If the length is not fixed use /@\\d+|(\\d+)/g .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM