简体   繁体   English

包含一个或多个指定字母的字符串的Javascript正则表达式

[英]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: 我正在使用以下正则表达式在字符串的任何位置的英文单词数组中搜索包含字母C,A,L,I,R,E,T,N的所有字符串:

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: 这是我的JavaScript( https://jsfiddle.net/jarosciak/7tk4aL0h/ ),它在搜索英语单词数组时演示了以上示例:脚本将成功找到单词:

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: 这对我来说是个不错的开始,但是我确实需要Regex表达式来查找包含我指定的大多数字母的所有单词(字符串):

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. 因此,例如,如果我指定字母:X,C,L,A,R,I,N,E,T。它应该找到单词:CLARINET,即使我也将X指定为以下字母之一搜索。

I can do this in SQL without any issues, but I can't figure out how to do this in REGEX. 我可以在SQL中做到这一点而没有任何问题,但是我不知道如何在REGEX中做到这一点。 Here is a working SQL example if that helps: 这是一个工作的SQL示例,如果有帮助的话:

在此处输入图片说明

Following Regex will find only Carinet in sequence. 遵循Regex的将仅按顺序查找Carinet。 Your question does not mention that if CLARINET will come in sequence or it will be in any order like CLRIATEN. 您的问题没有提到CLARINET是按顺序出现还是按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. 您可以通过添加/ i区分大小写来更改正则表达式。

Regex: '(CLARINET)+' 正则表达式: '(CLARINET)+'

If you think word can come anywhere in the text like some random data which contains all these words, you can use following. 如果您认为单词可以出现在文本中的任何地方,例如包含所有这些单词的一些随机数据,则可以使用following。

Regex: '[CLARINET]+' 正则表达式: '[CLARINET]+'

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

相关问题 包含一个或多个a的字符串的正则表达式 - Regular expression of a string that contains one or more a's 正则表达式Javascript匹配字母,除了一个 - Regular expression Javascript match letters except one 正则表达式检测到字符串同时包含字母和数字 - Regular expression detect that string contains both letters and numbers Javascript 用于电话号码验证的正则表达式。(字符串仅包含至少 1 个数字、0 个或多个空格和 '-' - Javascript regular expression for phone number validation.(String contains only at least 1 digit, 0 or more spaces and '-' 正则表达式,除字母外(javascript) - Regular expression anything but letters (javascript) 正则表达式javascript数字和字母 - regular expression javascript numbers and letters 在match的正则表达式上指示的Javascript match()字符串字母 - Javascript match() string letters which are indicated on regular expression of match 如何使用JavaScript正则表达式检查字符串是否包含至少一个字母? - How to check whether a String contains at least one letter using JavaScript Regular Expression? 用于检查字符串是否具有两个指定单词的正则表达式 - Regular expression for check if string has two specified words javascript 如何在javascript中测试字符串是否包含带有正则表达式的数字? - How to test if a string contains a number with regular expression in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM