Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Regular expression to match a string having string inside parenthisis

I want a create a regular expression that matches the certain rules.

I found this RE – /^.*\(.*\).*$/g but i also wanted to have validation on second string inside parenthesis.

eg-

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 United Kingdom (English)

 Belgium (Dutch)

ie- expression for [string space string inside parenthesis with at least 3 length of inside string.].

Thank you in advance.

>Solution :

In JavaScript:

const pattern = /^[A-Za-z\s]+\([A-Za-z]{3,}\)$/;
const str1 = 'United Kingdom (English)';
const str2 = 'Belgium (Dutch)';

console.log(pattern.test(str1)); // true
console.log(pattern.test(str2)); // true

Explanation of the pattern:

^ asserts the start of the string.
[A-Za-z\s]+ matches one or more letters or spaces.
\( matches the opening parenthesis.
[A-Za-z]{3,} matches three or more letters (assumed to be the inside string you mentioned).
\) matches the closing parenthesis.
$ asserts the end of the string.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading