简体   繁体   English

找到模式并动态构建正则表达式以匹配字符串

[英]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. 如果模式中存在星号*,则表示长度为3的相同字符的序列,除非其后接{N},该序列表示应在序列中出现多少个字符,其中N至少为1。我的目标是确定第二个字符串是否与输入中第一个字符串的模式完全匹配。 I'm having trouble building the Regex pattern 我在建立Regex模式时遇到问题

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

https://jsfiddle.net/82smw9zx/ 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). 您需要使用new RegExp()来用变量构造正则表达式模式(而不是尝试在正则表达式文字中直接包含变量)。

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. 您试图在正则表达式文字中包括变量queryStrSubStr.charAt(0)patternCount :/ /'queryStrSubStr.charAt(0){patternCount}'/ queryStrSubStr.charAt(0) patternCount /'queryStrSubStr.charAt(0){patternCount}'/ ,但是JavaScript不会将这些字符串解释为文字中的变量。

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. 以下示例演示了如何使用变量构造正则表达式模式,以及如何将小提琴中的html输入并入,以便可以测试各种模式。 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);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM