[英]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.