0

Example:

Here is my code of C#

This is regular expression demo in C# in dotfiddle.

string[] array = {"704000233873", "705000233873", "706000233873", "707000233873", "504000233873", "504000233873", "703000233873", "704000233873"};
string regex = @"^(50|70)(4|5)\d{9}$";
foreach (var item in array)
{
    Console.WriteLine(Regex.IsMatch(item, regex));
}

and Results are below:

True
True
False
False
True
True
False
True

When I try the same RegEx on Javascript JS or Chrome it returns false.

$(function() {
    var $checker = $('#checker');

    $checker.click(function(ev) {
        var inputFieldVal = $.trim($('#in').val());
        console.log(inputFieldVal); // Alg
        var regExpPattern = '^(50|70)(4|5)\d{9}$',
            re = new RegExp(regExpPattern);
        console.log(re); // /^Al/gim
        // Get text out of div#x
        var text = $('#x').text();
        // Trim and 'convert' to an array...
        text = $.trim(text).split('\n');
        console.log(text); // ["Aldor", "Aleph", "Algae", "Algo", "Algol", "Alma-0", "Alphard", "Altran"]
        for (var index = 0, upper = text.length; index < upper; ++index) {
            console.log(
            re.test(text[index].trim()), text[index]);
        }
    });
})

<form>
    <input id="in" />
</form>
<div id="x">
    704000233873
705000233873
706000233873
707000233873
504000233873
504000233873
703000233873
704000233873
</div><button id="checker">check!</button>

And the results are below.

["704000233873", "705000233873", "706000233873", "707000233873", "504000233873", "504000233873", "703000233873", "704000233873"]
false, "704000233873"
false, "705000233873"
false, "706000233873"
false, "707000233873"
false, "504000233873"
false, "504000233873"
false, "703000233873"
false, "704000233873"

What am I missing or why they return false?

Any advice?

David
  • 75
  • 7
  • There are some differences between regular expression in c# and js https://keestalkstech.com/2010/11/how-to-use-regex-groups-in-javascript/ – kogonidze May 20 '21 at 08:10
  • `'^(50|70)(4|5)\d{9}$'` -> `'^(50|70)(4|5)\\d{9}$'` (or use a regex literal) – Andreas May 20 '21 at 08:14

1 Answers1

0

You need to escape \d in your javascript for it to be equivalent to the C# regex. It should be like this: '^(50|70)(4|5)\\d{9}$'. In your C# code you prefixed the string with a @ which makes this unnecessary there.

If you want these as similar as possible to avoid confusion, you could change your C# pattern to string regex = "^(50|70)(4|5)\\d{9}$";.

Karl-Johan Sjögren
  • 16,544
  • 7
  • 59
  • 68
  • 1
    Don't you think that there already will be a similar question with "you have to escape special characters when used with the `RegExp` constructor"? – Andreas May 20 '21 at 08:12
  • 1
    Alternatively, since the regex is fixed in the code, there's no need to use the `new RegExp` construction, and instead you could just use `var re = /^(50|70)(4|5)\d{9}$/;` – James Thorpe May 20 '21 at 08:13
  • I read that with "//" I didn't know that applies also to "\\", My bad. – David May 20 '21 at 08:14
  • @Andreas I almost forgot that! But it made sense. – David May 20 '21 at 08:15