find the pattern and dynamically build regex to match the string
up vote
0
down vote
favorite
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;
javascript jquery regex
add a comment |
up vote
0
down vote
favorite
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;
javascript jquery regex
Do you try to call JS in your pattern or it's part of pattern string?
– Justinas
Nov 12 at 14:57
Take a look at the Regex Object Constructor if you want to dynamically create patterns.
– AutomatedChaos
Nov 12 at 15:02
@Justinas -- jsfiddle.net/82smw9zx
– dragonfly
Nov 12 at 16:02
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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;
javascript jquery regex
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;
javascript jquery regex
javascript jquery regex
edited Nov 12 at 14:52
georg
143k33193290
143k33193290
asked Nov 12 at 14:39
dragonfly
97861539
97861539
Do you try to call JS in your pattern or it's part of pattern string?
– Justinas
Nov 12 at 14:57
Take a look at the Regex Object Constructor if you want to dynamically create patterns.
– AutomatedChaos
Nov 12 at 15:02
@Justinas -- jsfiddle.net/82smw9zx
– dragonfly
Nov 12 at 16:02
add a comment |
Do you try to call JS in your pattern or it's part of pattern string?
– Justinas
Nov 12 at 14:57
Take a look at the Regex Object Constructor if you want to dynamically create patterns.
– AutomatedChaos
Nov 12 at 15:02
@Justinas -- jsfiddle.net/82smw9zx
– dragonfly
Nov 12 at 16:02
Do you try to call JS in your pattern or it's part of pattern string?
– Justinas
Nov 12 at 14:57
Do you try to call JS in your pattern or it's part of pattern string?
– Justinas
Nov 12 at 14:57
Take a look at the Regex Object Constructor if you want to dynamically create patterns.
– AutomatedChaos
Nov 12 at 15:02
Take a look at the Regex Object Constructor if you want to dynamically create patterns.
– AutomatedChaos
Nov 12 at 15:02
@Justinas -- jsfiddle.net/82smw9zx
– dragonfly
Nov 12 at 16:02
@Justinas -- jsfiddle.net/82smw9zx
– dragonfly
Nov 12 at 16:02
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
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
*/
I entered * {5} * aaaaaBBB'. Your code doesn't return desired output
– dragonfly
Nov 12 at 17:20
yes I did. When I added comments * is disappearing. Try it
– dragonfly
Nov 12 at 17:23
I have new RegExp in my code as well.
– dragonfly
Nov 12 at 17:24
can you please explain else { patt = new RegExp(text.charAt(0) + '{3}'); }
– dragonfly
Nov 12 at 17:28
@dragonfly - answer edited with more explanation that hopefully clears it up.
– benvc
Nov 12 at 17:35
|
show 4 more comments
up vote
0
down vote
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);
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
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
*/
I entered * {5} * aaaaaBBB'. Your code doesn't return desired output
– dragonfly
Nov 12 at 17:20
yes I did. When I added comments * is disappearing. Try it
– dragonfly
Nov 12 at 17:23
I have new RegExp in my code as well.
– dragonfly
Nov 12 at 17:24
can you please explain else { patt = new RegExp(text.charAt(0) + '{3}'); }
– dragonfly
Nov 12 at 17:28
@dragonfly - answer edited with more explanation that hopefully clears it up.
– benvc
Nov 12 at 17:35
|
show 4 more comments
up vote
1
down vote
accepted
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
*/
I entered * {5} * aaaaaBBB'. Your code doesn't return desired output
– dragonfly
Nov 12 at 17:20
yes I did. When I added comments * is disappearing. Try it
– dragonfly
Nov 12 at 17:23
I have new RegExp in my code as well.
– dragonfly
Nov 12 at 17:24
can you please explain else { patt = new RegExp(text.charAt(0) + '{3}'); }
– dragonfly
Nov 12 at 17:28
@dragonfly - answer edited with more explanation that hopefully clears it up.
– benvc
Nov 12 at 17:35
|
show 4 more comments
up vote
1
down vote
accepted
up vote
1
down vote
accepted
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
*/
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
*/
$('.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>
$('.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>
edited Nov 12 at 19:58
answered Nov 12 at 16:01
benvc
3,2821319
3,2821319
I entered * {5} * aaaaaBBB'. Your code doesn't return desired output
– dragonfly
Nov 12 at 17:20
yes I did. When I added comments * is disappearing. Try it
– dragonfly
Nov 12 at 17:23
I have new RegExp in my code as well.
– dragonfly
Nov 12 at 17:24
can you please explain else { patt = new RegExp(text.charAt(0) + '{3}'); }
– dragonfly
Nov 12 at 17:28
@dragonfly - answer edited with more explanation that hopefully clears it up.
– benvc
Nov 12 at 17:35
|
show 4 more comments
I entered * {5} * aaaaaBBB'. Your code doesn't return desired output
– dragonfly
Nov 12 at 17:20
yes I did. When I added comments * is disappearing. Try it
– dragonfly
Nov 12 at 17:23
I have new RegExp in my code as well.
– dragonfly
Nov 12 at 17:24
can you please explain else { patt = new RegExp(text.charAt(0) + '{3}'); }
– dragonfly
Nov 12 at 17:28
@dragonfly - answer edited with more explanation that hopefully clears it up.
– benvc
Nov 12 at 17:35
I entered * {5} * aaaaaBBB'. Your code doesn't return desired output
– dragonfly
Nov 12 at 17:20
I entered * {5} * aaaaaBBB'. Your code doesn't return desired output
– dragonfly
Nov 12 at 17:20
yes I did. When I added comments * is disappearing. Try it
– dragonfly
Nov 12 at 17:23
yes I did. When I added comments * is disappearing. Try it
– dragonfly
Nov 12 at 17:23
I have new RegExp in my code as well.
– dragonfly
Nov 12 at 17:24
I have new RegExp in my code as well.
– dragonfly
Nov 12 at 17:24
can you please explain else { patt = new RegExp(text.charAt(0) + '{3}'); }
– dragonfly
Nov 12 at 17:28
can you please explain else { patt = new RegExp(text.charAt(0) + '{3}'); }
– dragonfly
Nov 12 at 17:28
@dragonfly - answer edited with more explanation that hopefully clears it up.
– benvc
Nov 12 at 17:35
@dragonfly - answer edited with more explanation that hopefully clears it up.
– benvc
Nov 12 at 17:35
|
show 4 more comments
up vote
0
down vote
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);
add a comment |
up vote
0
down vote
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);
add a comment |
up vote
0
down vote
up vote
0
down vote
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);
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);
answered Nov 12 at 17:34
Poul Bak
5,1633832
5,1633832
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53264463%2ffind-the-pattern-and-dynamically-build-regex-to-match-the-string%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Do you try to call JS in your pattern or it's part of pattern string?
– Justinas
Nov 12 at 14:57
Take a look at the Regex Object Constructor if you want to dynamically create patterns.
– AutomatedChaos
Nov 12 at 15:02
@Justinas -- jsfiddle.net/82smw9zx
– dragonfly
Nov 12 at 16:02