简体   繁体   English

正则表达式匹配除特定标志格式的单词之外的所有内容

[英]Regex Match everything except words of particular flag format

I need a regex that can match everything else except the random flags..我需要一个可以匹配除随机标志之外的所有其他内容的正则表达式..

All flags have this format, starts with FLAG and ends in ;所有标志都具有这种格式,以FLAG开头并以;结尾

FLAG:random_token;

Example:例子:

hello
hello world
lorem ipsum dolor sit amet
FLAG:xyz6767abcd45xyz; and lorem
lorem ipsum dolor
FLAG:abc123; and hello there,..
hello there....

output Im trying to obtain: output 我试图获得:

hello
hello world
lorem ipsum dolor sit amet
 and lorem
lorem ipsum dolor
 and hello there,..
hello there....

So far I've tried:到目前为止,我已经尝试过:
^(?:FLAG.(?*;).).*
and
(?.:*\bFLAG..*$)^.*$

But it fails to extract the strings after the semicolon in FLAG:random_token;但它无法提取 FLAG:random_token 中分号后的字符串;
Any help would be appreciated任何帮助,将不胜感激

And I've tried deleting all Flags from the block, but I needed the token values later and Also thought regex would be the best fit.而且我已经尝试从块中删除所有标志,但我稍后需要令牌值并且还认为正则表达式是最合适的。

One way to do this would be to remove the flags from the input string, using String.replace and a regex to match the FLAG: and random token (everything to the next ; ), you can then use a callback function to store the tokens as they are found:一种方法是从输入字符串中删除标志,使用String.replace和正则表达式来匹配FLAG:和随机令牌(一切都到下一个; ),然后您可以使用回调 function 来存储令牌因为他们被发现:

 str = `hello hello world lorem ipsum dolor sit amet FLAG:xyz6767abcd45xyz; and lorem lorem ipsum dolor FLAG:abc123; and hello there,.. hello there....`; const tokens = []; str = str.replace(/FLAG:([^;]+);/g, (_, p1) => { tokens.push(p1); return ''; }); console.log(str); console.log(tokens);

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

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