0

I have the following PHP regex:

/\b([A-Z0-9]{8})\b/i

in which I wanted to convert to a javascript regex. So I tried the following:

/^(\b([A-Z0-9]{8})\b/i

however it failed. Why is this? I am trying to check if a string has a word that contains 8 digit of alphanumeric character, ignoring uppercase and lowercase (meaning that 2HJS1289 and 2hjs1289 should match). The string needs to have 8 digits.

3
  • 1
    First of all, why did you make any changes at all? Commented Dec 4, 2013 at 10:57
  • have you tried the PHP one verbatim? Commented Dec 4, 2013 at 10:57
  • 1
    define "it failed". what did you test, what happened? Anyway, I see 2 open brackets ((), and only one closeing? Commented Dec 4, 2013 at 10:57

2 Answers 2

2

You're missing a ):

/^(\b([A-Z0-9]{8})\b)/i

Although, you can just use the same expression as you would for PHP.

1
var regex = /^(\b([A-Z0-9]{8})\b)/i;
var _string = "2HJS1289";

if( regex.test(_string) ){
    alert('pass');
}
else{
    alert('fail');
}

http://jsfiddle.net/jogesh_pi/2A4tA/

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.