0

i am newbie to regex in javascript and i got stuck in using regex in javascript.

I have two scenerio

1) I have a string like aaa/+/bbb/+ , i want to match this string with strings aaa/1/bbb/hello123 and aaa/1/bbb/ using regex and both should return true. If i pass aaa/1/bbb then it should give false

2) If i have string aaa/# then it should match all the above strings and returmn true

Can any one help me?

Qasim
  • 9,058
  • 8
  • 36
  • 50
  • Can you be more specific about what the significance of `+` and `#` is? It looks like `+` is intended to match one or more numbers and letters, but it's not clear. – 4castle Aug 10 '16 at 03:26
  • `2) If i have string aaa/# then it should match all the above strings and returmn true` - no it shouldn't – Jaromanda X Aug 10 '16 at 03:29
  • @JaromandaX Can you explain? They are simply defining custom wildcard characters. I don't see how `#` shouldn't be aloud to have that meaning. – 4castle Aug 10 '16 at 03:33
  • @4castle - one does not make ones own rules for how RegExp works ... `aaa/#` will not match the strings given. But I think I understand where I went wrong, I assumed he meant that `aaa/#` was actually a RegExp - re-reading the question, I see he wants someone to write some code for his own pattern matching scheme, possibly using RegExp as required - this is not icanhazcode.com so I wish him the best of luck in his quest to get free programming done – Jaromanda X Aug 10 '16 at 03:36
  • `i got stuck in using regex` - you sure have, @Qasim, you haven't got ANY code at all - good luck – Jaromanda X Aug 10 '16 at 03:37
  • + means only upto one level and # means there may be multiple levels followed by slashes – Qasim Aug 10 '16 at 11:33

2 Answers2

1

In regex, the + would translate to [^/]*, and the # would translate to .*.

The next step is to escape special characters in the input string. This regex has that purpose. Since the + is a special character, we will have to unescape manually.

RegExp.escape= function(s) {
    return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
};

function matchesPattern(pattern, input) {
  pattern = RegExp.escape(pattern);            // escape special characters
  pattern = pattern.replace(/\\\+/g, "[^/]*"); // replace '\+'
  pattern = pattern.replace(/#/g, ".*");     // replace '#'
  pattern = new RegExp("^" + pattern + "$");   // construct regex
  return pattern.test(input);                  // test input
}

console.log(matchesPattern("aaa/+/bbb/+", "aaa/1/bbb/hello123"));
console.log(matchesPattern("aaa/+/bbb/+", "aaa/1/bbb/"));
console.log(matchesPattern("aaa/+/bbb/+", "aaa/1/bbb"));

console.log(matchesPattern("aaa/#", "aaa/1/bbb/hello123"));
console.log(matchesPattern("aaa/#", "aaa/1/bbb/"));
console.log(matchesPattern("aaa/#", "aaa/1/bbb"));
Community
  • 1
  • 1
4castle
  • 32,613
  • 11
  • 69
  • 106
0

The regular expression ^aaa\/[0-9]+\/bbb\/[\w\d]*$ will match the following strings:

aaa/1/bbb/hello123
aaa/1/bbb/

These strings will not be matched:

aaa/1/bbb

I am unclear about your second scenario - are you saying that the regex should also match the string aaa/#?

You can use regex in javascript like so:

var re = /^aaa\/[0-9]+\/bbb\/[\w\d]*$/;
var matches = re.test("aaa/1/bbb/");
if (matches) { console.log("it matches!"); }
Carl Sverre
  • 1,139
  • 10
  • 19