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?

6
  • 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. Commented Aug 10, 2016 at 3:26
  • 2) If i have string aaa/# then it should match all the above strings and returmn true - no it shouldn't Commented Aug 10, 2016 at 3: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. Commented Aug 10, 2016 at 3: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 Commented Aug 10, 2016 at 3:36
  • i got stuck in using regex - you sure have, @Qasim, you haven't got ANY code at all - good luck Commented Aug 10, 2016 at 3:37

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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!"); }

2 Comments

Use re.test(str) in order to see if there is a match.
Thanks 4castle - forgot about that method, will update.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.