-1

This is the continuation of php regex: phone number 7-12 digits may contain hypen or space

I'm still trying to understand backreferences. The following is the JavaScript code I came up with to test backreferences. When I run this code only null is being outputted. Where am I going wrong?

<script type="text/javascript">
function myRegexFunc()
{   myString="abcabcxabcdefghi";
    patt1=new RegExp("(abc)?\1x\1");
    document.write(patt1.exec(myString));
}
</script>
</head>

<body onload="myRegexFunc()">
</body>
Community
  • 1
  • 1
vaanipala
  • 1,261
  • 7
  • 36
  • 63

1 Answers1

3

You need to escape the \ in the string.

patt1 = new RegExp("(abc)?\\1x\\1");

Or you could use the regex literal:

patt1 = /(abc)?\1x\1/;
xdazz
  • 158,678
  • 38
  • 247
  • 274