简体   繁体   中英

Multiple character lookup within square brackets with regex

I'm using regex in JavaScript for certain text replacements to convert legacy encoded text to unicode (it's an indic language). Suppose I anywhere I find either of a,b,c followed by either of x,y,z followed by e I have to replace it so that e comes first. So I have code like this:

modified_substring = modified_substring.replace( /([abc])([xyz]*)e/g , "e$1$2" ) ;

Now let us say I want to modify this rule as a or b or c or klm followed by either of x , y , z followed by e . So what would the code be?

modified_substring = modified_substring.replace( /([abc]klm)([xyz]*)e/g , "e$1$2" ) ;

That apparently doesn't work. Is there a way to do this?

You need to use alternation operator | .

modified_substring = modified_substring.replace( /([abc]|klm)([xyz]*)e/g , "e$1$2" ) ;
                                                        ^

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