dragonfly
dragonfly

Reputation: 3227

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;

Upvotes: 0

Views: 1631

Answers (2)

benvc
benvc

Reputation: 15120

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(() => {
  const 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); // return true if text matches pattern or false if not 
    
  } else { // if input string does not start with *  
    return 'No pattern';    
  }
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" class="enter_pattern" />
<button type="submit" class="btn">Click</button>

/*
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
*/

Upvotes: 1

Poul Bak
Poul Bak

Reputation: 10929

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);

Upvotes: 0

Related Questions