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












0















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:



enter image description here










share|improve this question























  • The value of @regex in your SQL already contains the regular expression you want. So, why don't you use that in your Javascript?

    – Corion
    Jan 3 at 13:43











  • What the most in the most of the letters qualifies? Are you matching the regex against multiple input strings?

    – revo
    Jan 3 at 13:49











  • Hi @revo, actually I am after getting a single words that matches the most letters I specified in the regex query, however that's not likely something I can do. Albeit, thinking of it, the longest letter should probably be the most correct answer.

    – jjj
    Jan 3 at 18:31
















0















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:



enter image description here










share|improve this question























  • The value of @regex in your SQL already contains the regular expression you want. So, why don't you use that in your Javascript?

    – Corion
    Jan 3 at 13:43











  • What the most in the most of the letters qualifies? Are you matching the regex against multiple input strings?

    – revo
    Jan 3 at 13:49











  • Hi @revo, actually I am after getting a single words that matches the most letters I specified in the regex query, however that's not likely something I can do. Albeit, thinking of it, the longest letter should probably be the most correct answer.

    – jjj
    Jan 3 at 18:31














0












0








0








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:



enter image description here










share|improve this question














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:



enter image description here







javascript sql regex






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 3 at 13:38









jjjjjj

83841637




83841637













  • The value of @regex in your SQL already contains the regular expression you want. So, why don't you use that in your Javascript?

    – Corion
    Jan 3 at 13:43











  • What the most in the most of the letters qualifies? Are you matching the regex against multiple input strings?

    – revo
    Jan 3 at 13:49











  • Hi @revo, actually I am after getting a single words that matches the most letters I specified in the regex query, however that's not likely something I can do. Albeit, thinking of it, the longest letter should probably be the most correct answer.

    – jjj
    Jan 3 at 18:31



















  • The value of @regex in your SQL already contains the regular expression you want. So, why don't you use that in your Javascript?

    – Corion
    Jan 3 at 13:43











  • What the most in the most of the letters qualifies? Are you matching the regex against multiple input strings?

    – revo
    Jan 3 at 13:49











  • Hi @revo, actually I am after getting a single words that matches the most letters I specified in the regex query, however that's not likely something I can do. Albeit, thinking of it, the longest letter should probably be the most correct answer.

    – jjj
    Jan 3 at 18:31

















The value of @regex in your SQL already contains the regular expression you want. So, why don't you use that in your Javascript?

– Corion
Jan 3 at 13:43





The value of @regex in your SQL already contains the regular expression you want. So, why don't you use that in your Javascript?

– Corion
Jan 3 at 13:43













What the most in the most of the letters qualifies? Are you matching the regex against multiple input strings?

– revo
Jan 3 at 13:49





What the most in the most of the letters qualifies? Are you matching the regex against multiple input strings?

– revo
Jan 3 at 13:49













Hi @revo, actually I am after getting a single words that matches the most letters I specified in the regex query, however that's not likely something I can do. Albeit, thinking of it, the longest letter should probably be the most correct answer.

– jjj
Jan 3 at 18:31





Hi @revo, actually I am after getting a single words that matches the most letters I specified in the regex query, however that's not likely something I can do. Albeit, thinking of it, the longest letter should probably be the most correct answer.

– jjj
Jan 3 at 18:31












1 Answer
1






active

oldest

votes


















1














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]+'






share|improve this answer
























  • The first thing is clearly wrong as the OP wants to specify XCLARINET and still match CLARINET. Your approach can be improved, as currently your approach will also allow fooCfoo , as it is unanchored.

    – Corion
    Jan 3 at 14:03













  • The second regex is likely the best thing I've got so far. I was able to amend my script to get the first and best word, containing the most characters I was searching for, it's here: jsfiddle.net/jarosciak/u3oy6zd0

    – jjj
    Jan 3 at 18:53











  • I am glad that it helped.

    – Deep
    Jan 3 at 19:08












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54023414%2fjavascript-regular-expression-for-a-string-that-contains-one-or-more-of-specifie%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














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]+'






share|improve this answer
























  • The first thing is clearly wrong as the OP wants to specify XCLARINET and still match CLARINET. Your approach can be improved, as currently your approach will also allow fooCfoo , as it is unanchored.

    – Corion
    Jan 3 at 14:03













  • The second regex is likely the best thing I've got so far. I was able to amend my script to get the first and best word, containing the most characters I was searching for, it's here: jsfiddle.net/jarosciak/u3oy6zd0

    – jjj
    Jan 3 at 18:53











  • I am glad that it helped.

    – Deep
    Jan 3 at 19:08
















1














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]+'






share|improve this answer
























  • The first thing is clearly wrong as the OP wants to specify XCLARINET and still match CLARINET. Your approach can be improved, as currently your approach will also allow fooCfoo , as it is unanchored.

    – Corion
    Jan 3 at 14:03













  • The second regex is likely the best thing I've got so far. I was able to amend my script to get the first and best word, containing the most characters I was searching for, it's here: jsfiddle.net/jarosciak/u3oy6zd0

    – jjj
    Jan 3 at 18:53











  • I am glad that it helped.

    – Deep
    Jan 3 at 19:08














1












1








1







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]+'






share|improve this answer













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]+'







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 3 at 13:58









DeepDeep

193211




193211













  • The first thing is clearly wrong as the OP wants to specify XCLARINET and still match CLARINET. Your approach can be improved, as currently your approach will also allow fooCfoo , as it is unanchored.

    – Corion
    Jan 3 at 14:03













  • The second regex is likely the best thing I've got so far. I was able to amend my script to get the first and best word, containing the most characters I was searching for, it's here: jsfiddle.net/jarosciak/u3oy6zd0

    – jjj
    Jan 3 at 18:53











  • I am glad that it helped.

    – Deep
    Jan 3 at 19:08



















  • The first thing is clearly wrong as the OP wants to specify XCLARINET and still match CLARINET. Your approach can be improved, as currently your approach will also allow fooCfoo , as it is unanchored.

    – Corion
    Jan 3 at 14:03













  • The second regex is likely the best thing I've got so far. I was able to amend my script to get the first and best word, containing the most characters I was searching for, it's here: jsfiddle.net/jarosciak/u3oy6zd0

    – jjj
    Jan 3 at 18:53











  • I am glad that it helped.

    – Deep
    Jan 3 at 19:08

















The first thing is clearly wrong as the OP wants to specify XCLARINET and still match CLARINET. Your approach can be improved, as currently your approach will also allow fooCfoo , as it is unanchored.

– Corion
Jan 3 at 14:03







The first thing is clearly wrong as the OP wants to specify XCLARINET and still match CLARINET. Your approach can be improved, as currently your approach will also allow fooCfoo , as it is unanchored.

– Corion
Jan 3 at 14:03















The second regex is likely the best thing I've got so far. I was able to amend my script to get the first and best word, containing the most characters I was searching for, it's here: jsfiddle.net/jarosciak/u3oy6zd0

– jjj
Jan 3 at 18:53





The second regex is likely the best thing I've got so far. I was able to amend my script to get the first and best word, containing the most characters I was searching for, it's here: jsfiddle.net/jarosciak/u3oy6zd0

– jjj
Jan 3 at 18:53













I am glad that it helped.

– Deep
Jan 3 at 19:08





I am glad that it helped.

– Deep
Jan 3 at 19:08




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54023414%2fjavascript-regular-expression-for-a-string-that-contains-one-or-more-of-specifie%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas