1

I ma trying to match following exact format: '7b3bda24-31db-44d4-aa7f-7012b3594623' but without success.

var reg = new RegExp('/^([a-f0-9]{8})-(([a-f0-9]{4})-){3}([a-f0-9]{12})$/i');

document.getElementById('result').innerHTML = reg.test('7b3bda24-31db-44d4-aa7f-7012b3594623')
<p id="result"></p>

2
  • Replace (([a-f0-9]{4})-){3} with ([a-f0-9]{4})-([a-f0-9]{4})- Commented Sep 12, 2022 at 19:31
  • It is either var reg = new RegExp(/^([a-f0-9]{8})-(([a-f0-9]{4})-){3}([a-f0-9]{12})$/i); or var reg = new RegExp('^([a-f0-9]{8})-(([a-f0-9]{4})-){3}([a-f0-9]{12})$', 'i'); Commented Sep 12, 2022 at 20:15

1 Answer 1

3

The RegExp constructor accepts a string regular expression (no slashes). If your regular expression is not dynamic, just use a regular expression literal.

const reg = /^([a-f0-9]{8})-(([a-f0-9]{4})-){3}([a-f0-9]{12})$/i;
console.log(reg.test('7b3bda24-31db-44d4-aa7f-7012b3594623'));

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

Comments

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.