简体   繁体   English

正则表达式用于JavaScript中的令牌替换

[英]Regex for token replacement in JavaScript

I have a very specific requirement in JavaScript, and I'm wondering if anyone can help. 我对JavaScript有一个非常特定的要求,我想知道是否有人可以提供帮助。

I'm looking for a validation Regex that I can use to validate tokens in a string. 我正在寻找一个可用来验证字符串中的令牌的验证正则表达式。 Each token is in the following format: 每个令牌的格式如下:

(__([A-Z1-9_])+__)

IOW, two underscores, following by any number of upper-case characters, digits or underscores, and ending with another two underscores. IOW,两个下划线,后跟任意数量的大写字符,数字或下划线,并以另外两个下划线结尾。 Examples might be: 例如:

__A_TOKEN__
__ANOTHER_1T_O_K_E_N__

A token cannot contain two underscores, so the following would be invalid: 令牌不能包含两个下划线,因此以下内容将无效:

__A__TOKEN__

Finally, there could be multiple tokens in the string, with or without other leading or trailing characters, like this: 最后,字符串中可能有多个标记,带有或不带有其他前导或尾随字符,如下所示:

http://__DOMAIN__/__PATH__/logs/__LOGFILE__.log

Theoretically, there could even be two tokens right next to one another, eg: 从理论上讲,甚至可能有两个标记彼此相邻,例如:

__TOKEN_A____TOKEN_B__

I've been playing with regex's for a while, but this has me stumped as to how to validate. 我玩正则表达式已经有一段时间了,但这让我很困惑如何进行验证。 It's easy enough to simply check for a single token with optional leading/trailing text, with this: 使用以下可选的开头/结尾文本来简单地检查单个令牌是很容易的:

^(.*)?(__([A-Z1-9_])+__)+(.*)?$

but how can I check for multiple tokens, like this (which will validate using the above regex): 但是我该如何检查多个令牌,如下所示(它将使用上述正则表达式进行验证):

xxx__UPPERCASE__yyy__lowercase__ xxx__大写__yyy__小写__

which shouldn't be allowed, since __lowercase__ isn't a valid token. 由于__lowercase__不是有效的令牌,因此不应使用。

Is this something which is best validated programatically rather than with a regex? 这是最好通过编程而不是使用正则表达式进行验证的东西吗?

I have a solution for you: 我为您提供解决方案:

/__([A-Z\d]+(?:_[A-Z\d]+)*)__/g

Yes, there is a repetition there, but it's straight forward to change. 是的,那里有重复,但是改变很简单。

You can check it here in action on this link: http://regex101.com/r/zZ6cC3/1 您可以在此处通过以下链接查看它: http : //regex101.com/r/zZ6cC3/1

This takes into account the rules you provided by using a negative look behind. 这考虑到了您使用否定性后面观察提供的规则。

var str = "__TOKEN_A____TOKEN_B__"; 
var res = str.match(/__(?:(?!__)[A-Z1-9_])+__/g);
alert(res);

Regex 正则表达式

/__(?:(?!__)[A-Z1-9_])+__/g

Explanation 说明

__            -- Match the two underscores (the start of a token)
(?:           -- Non-capturing group
 (?!__)       -- Negative look behind to check if we're starting a new token
 [A-Z1-9_]    -- Valid characters within the token
)+            -- Allow one or more valid characters
__            -- The ending token sequence of two underscores

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

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