简体   繁体   中英

Javascript Regular Expression for a string that contains one or more of specified letters

I am using the following Regex to search the array of English words for all strings that contains letters C, A, L, I, R, E, T, N anywhere in the body of the string:

const regex = /\b(?=\w*C)(?=\w*A)(?=\w*L)(?=\w*I)(?=\w*R)(?=\w*E)(?=\w*T)(?=\w*N)\w+/ig;

Here is my JavaScript ( https://jsfiddle.net/jarosciak/7tk4aL0h/ ) that demonstrates the above example when searching in an array of English words: The script will successfully find the words:

CLARINET

This is a good start for me, but I really need the Regex expression that finds all the words (strings) that contains the most of the letters I specify:

So for example, if I specify the letters: X, C, L, A, R, I, N, E, T. It should find the word: CLARINET, even though I also specified the X as a one of the letters to search for.

I can do this in SQL without any issues, but I can't figure out how to do this in REGEX. Here is a working SQL example if that helps:

在此处输入图片说明

Following Regex will find only Carinet in sequence. Your question does not mention that if CLARINET will come in sequence or it will be in any order like CLRIATEN. But as per your data and my assumption that it will come in sequence, here you go. You can change the regex by adding /i for case insensitivity.

Regex: '(CLARINET)+'

If you think word can come anywhere in the text like some random data which contains all these words, you can use following.

Regex: '[CLARINET]+'

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