简体   繁体   English

如何匹配不包含特定字符的字符序列?

[英]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. 我的字符串中有一系列数字,我想用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. 我不知道该如何用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 @ ? 我知道我应该使用Lookahead来检查条件: (?!@[az])但是以上规则仅用于不选择@开头的数字,如何选择不使用@开头的数字呢? I need to say to RegEx to skip @\\d and rather select only (?!@\\d) . 我需要对RegEx说跳过@\\d ,而是仅选择(?!@\\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 . 并使用替换$1

If the length is not fixed use /@\\d+|(\\d+)/g . 如果长度不固定,则使用/@\\d+|(\\d+)/g

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 正则表达式,用于匹配两个字符(其中@是唯一字符或前面带有空格) - regex for match out of two characters where @ is the only character or preceded by a whitespace 匹配字符,但不匹配 - Match character but not when preceded by 匹配模式前面没有字符 - Match pattern not preceded by character 如果单词的第一个字符没有以特殊字符序列开头或前缀,如何将其大写? - How to uppercase the first character of a word if it was not preceded or prefixed by a special character sequence? 正则表达式以匹配空格字符,后跟空格字符 - Regex to match a space character preceded by a space character regexp以匹配序列(如果不是以特定字符开头和结尾) - regexp to match sequence if not starting and ending with a specific character 模仿否定的向后看以匹配 JavaScript 正则表达式中没有紧跟在特定字符之前的模式 - Mimicking negative lookbehind to match a pattern not immediately preceded with a specific character in JavaScript regex 如何仅匹配第一次出现的字母数字字符,前面是非字母数字字符? - How to match only the first occurrence of an alphanumeric character, preceded by a non-alphanumeric character? 匹配一个序列,除非它前面带有正则表达式(Javascript)的另一个序列 - Match a sequence unless it is preceded by another sequence with regex (Javascript) 正则表达式 - 获取特定字符前后的字母 - Regex - Getting letters preceded and followed by a specific character
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM