0

Acoording to https://regex101.com/ regexp '^[-+]?[0-9]*\.?[0-9]$' for strings like "g2", "a8" etc. (one digit preceded by one letter) should return "false" (no match), but if I use it in JavaScript using RegExp test method "true" is returned. What is the reason of that behaviour?

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction() {
    var str = "g2";
    var patt = new RegExp('^[-+]?[0-9]*\.?[0-9]$');
    var res = patt.test(str);
    document.getElementById("demo").innerHTML = res;
}
</script>

</body>
</html>
Springfield762
  • 221
  • 1
  • 11
  • 2
    Also, see http://stackoverflow.com/questions/10769964/backslashes-regular-expression-javascript, http://stackoverflow.com/questions/6521572/javascript-regex-not-working and http://stackoverflow.com/questions/6149770/what-is-the-difference-between-using-regexp-and-using-forward-slash-notation-to. – Wiktor Stribiżew Oct 04 '16 at 13:36
  • The takeaway here is: Wherever possible, use a regex *literal*: `var patt = /^[-+]?[0-9]*\.?[0-9]$/;` When not possible, remember that backslashes have special meaning in string literals. – T.J. Crowder Oct 04 '16 at 13:38
  • @WiktorStribiżew: What, only four? ;-) – T.J. Crowder Oct 04 '16 at 13:38
  • You code should be like `

    `
    – SPViradiya Oct 04 '16 at 13:39
  • @SPViradiya: You have quotes there you don't want. But really, the linked questions' answers answer the question just fine. – T.J. Crowder Oct 04 '16 at 13:40
  • @T.J.Crowder I have changed the reguler expression. I did not saw you have suggested it already and just changed it with needed slash and slag "g". – SPViradiya Oct 04 '16 at 13:41
  • @SPViradiya: Remove the quotes around the regex literal. But again, really best just to move on. – T.J. Crowder Oct 04 '16 at 13:42
  • 1
    @SPViradiya: If you test your regexps at [regex101.com](https://regex101.com/r/Z41koT/1), you might have noticed the *code generator* option on the left. Click it, and you will see the code snippet you can leverage in your code. – Wiktor Stribiżew Oct 04 '16 at 13:43
  • @T.J.Crowder: Yes, just these four are enough. My favorite is [Backslashes - Regular Expression - Javascript](http://stackoverflow.com/questions/10769964/backslashes-regular-expression-javascript). – Wiktor Stribiżew Oct 04 '16 at 13:46
  • Thank you for answers. Problem solved. – Springfield762 Oct 04 '16 at 13:46
  • Yes @T.J. Crowder you are right. It should be `var patt = new RegExp(/^[-+]?[0-9]*\.?[0-9]$/g); ` And Yes @Wiktor Stribizew it shows it – SPViradiya Oct 04 '16 at 13:46
  • @SPViradiya: *"It should be `var patt = new RegExp(/^[-+]?[0-9]*\.?[0-9]$/g);`"* OMG. No. See my very first comment above (immediately under Wiktor's "Also, see..." comment) for the correct syntax. – T.J. Crowder Oct 04 '16 at 13:48
  • @T.J.Crowder it works in this way too. You can check it buddy. :) – SPViradiya Oct 04 '16 at 13:50
  • @SPViradiya: Yes, it *works*, it's just pointless. There's zero reason to wrap a valid regex literal in `new RegExp()` in 2016 (there was, briefly, a reason back in, oh, 2005) unless you're intentionally making a *copy*, and there's no need for that here. – T.J. Crowder Oct 04 '16 at 13:53

0 Answers0