简体   繁体   中英

find the pattern and dynamically build regex to match the string

If asterisk * is present in the pattern, then it means a sequence of the same character of length 3 unless it is followed by {N} which represents how many characters should appear in the sequence where N will be at least 1. My goal is to determine if the second string exactly matches the pattern of the first string in the input. I'm having trouble building the Regex pattern

*{2}* mmRRR should return TRUE
*{2}* mRRR  should return FALSE

https://jsfiddle.net/82smw9zx/

sample code::

pattern1 = /'queryStrSubStr.charAt(0){patternCount}'/;
var patternMatch = new RegExp(pattern1);
if(queryStrSubStr.match(patternMatch)) {
    result =  true;
} else result =  false;

You need to use new RegExp() to construct your regex pattern with variables (rather than attempting to include a variable directly in your regular expression literal).

You are trying to include variables queryStrSubStr.charAt(0) and patternCount in a regular expression literal like: /'queryStrSubStr.charAt(0){patternCount}'/ , but JavaScript does not interpret those strings as variables inside the literal.

Following example demonstrates how to construct your regex pattern with variables as well as incorporating the html input from your fiddle so that you can test various patterns. Code comments explain how the code works.

 $('.btn').click(function() { let result = wildcards($('.enter_pattern').val()); console.log(result); }); const wildcards = (s) => { if (s.startsWith('*')) { // if input string starts with * let pattern; let [count, text] = s.split(' '); // split input string into count and text count = count.match(/\\{\\d+\\}/); // match count pattern like {n} if (count) { // if there is a count pattern = new RegExp(text.charAt(0) + count); // regex: first character + matched count pattern } else { // if there is no count pattern = new RegExp(text.charAt(0) + '{3}'); // regex: first character + default pattern {3} } return s.match(pattern) ? true : false; // return true if text matches pattern or false if not } else { // if input string does not start with * return 'No pattern'; } } 
 <input type="text" class="enter_pattern" /> <button type="submit" class="btn">Click</button> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

/*
Example test output:

Input: *{2}* mmRRR
Log: true

Input: *{2}* mRRR
Log: false

Input: * mmmRRR
Log: true

Input: * mmRRR
Log: false

Input: mmRRR
Log: No pattern
*/

First you need to calulate the pattern using a regex:

/\*\{(\d+)\}\*/

It matches a star, a left Square bracket, followed by one or more digits and ending with a right Square bracket and a star.

How to use:

var text = 'mmRRR';
var char = text.charAt(0);
var pattern = '*{2}*';
var counter = /\*\{(\d+)\}\*/.exec(pattern)[1] || '3';
var regex = new RegeExp('^' + char + '\{' + counter + '}$');
var result = text.match(regex);

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