4

I tried to search this question first but didn't found what I need, here is it:

I have a string like: "substring1/substring2.substring3" (e.g. "library/History.read")

I need a regular expression to check:

  1. the substring1 must be "library"
  2. the substring2 must have a captial letter at the beginning
  3. the substring3 must be "read"

I'm not familiar with regular expression, the current I wrote is: 'library/([a-zA-Z])\\.(read)' but it is not correct. Please help me, thanks!

2 Answers 2

3

There are many ways to solve regex problems, starting from your own attempt here's something that works! :-)

library/([A-Z][a-zA-Z]+)\.(read) 

You had three small mistakes:

  • . is a character class matching any character except newline. Escape your . like this \. not like this \\.; and
  • Your first capture group was matching exactly one letter. You missed the quantifier. Use the + qunatifier to match one or more.
  • As pointed out by Peter, you were missing a character class for your first capital letter
Sign up to request clarification or add additional context in comments.

4 Comments

Glad it helped! :-) Will you consider marking my question as the accepted answer? I'm new here I hope to get some reputation.
@Meilan I noticed you marked the other answer as accepted (in case it was a mistake...) Thanks!
Actually the regex does not account for initial caps letter. Should be library/([A-Z][a-zA-Z]*)\.(read)
Correct Peter! I only focused on the reason is regex wasn't matching instead of the requirements. Thanks
3

Try this regex:

testStrings = ["library/History.read", "library/other.read", "library/Geography.read", "library/History.unread", "ebook/hstory.read", "library/StackOverflow.read"] regex = /library\/[A-Z][^\.]*\.read/ testStrings.forEach(testString => { console.log(regex.test(testString) + " - " + testString) })

5 Comments

Thank you, I run this regex on the online compiler with the following code, but all of them return false, do you know why? rextester.com/l/js_online_compiler ```
``` const testStrings = ["library/History.read", "library/other.read", "library/Geography.read", "library/History.unread", "ebook/hstory.read", "library/StackOverflow.read"]; const pattern = new RegExp('/library\/[A-Z][^\.]*\.read/') for (var index = 0; index < 6; index++){ print(testStrings[index]); print(pattern.test(testStrings[index])); } ```
@Meilan: That online tool seems to support only older JavaScript. Change function call to: testStrings.forEach(function(testString) { .... Also, change console.log() to print()
The code does not work because you have to remove the / like const pattern = new RegExp('library\/[A-Z][^.]*\.read'); or remove the ' and keep the /. In that case, you can use const pattern = /library\/[A-Z][^.]*\.read/;
Off topic, but don't concat different values into a single string like that when you log them. Especially not different types. They may look the same in the output from the SO snippets, but not in the console. true, "true" and [true] are not the same thing, but will be serialized the same way. Check this: console.log(true + "," + "true" + "," + [true, true]) vs console.log(true, "true", [true, true]) Or use string substitutions: console.log("match: %o - %o", regex.test(testString), testString)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.