0

I have used Regex in C#. How do I use this type of Regex in Javasript. This type of regex works in C#.

I have the following Regex:

(?<Month>[A-Za-z]{3}) (?<Date>[0-9]{2}) (?<Hour>[0-9]{2}):(?<Minutes>[0-9]{2}):(?<Seconds>[0-9]{2}) (?<ComputerName>[A-Za-z0-9\-\ ]+): (?<KernalTime>[0-9\.\[\]\ ]+) (?<Message>[A-Za-z\(\)\.\ ]+)

to Parse the Following Line (All Lines are similar):

Nov 24 14:24:31 Dell-Inspiron-N5110 kernel: [    0.000000]   Intel GenuineIntel

In javascript i used the following code.

matchRegexParse: function () {



   var myString = "Nov 24 14:24:31 Dell-Inspiron-N5110 kernel: [    0.000000]   Intel GenuineIntel";
    var myRegexp = new RegExp(
        "(?<Month>[A-Za-z]{3}) (?<Date>[0-9]{2}) (?<Hour>[0-9]{2}):(?<Minutes>[0-9]{2}):(?<Seconds>[0-9]{2}) (?<ComputerName>[A-Za-z0-9\-\ ]+): (?<KernalTime>[0-9\.\[\]\ ]+) (?<Message>[A-Za-z0-9\(\)\.\ \-\:]+)",
        "gi");

    var match = myRegexp.exec(myString);
    alert(match);
},

I get the following Error (See Attached Image):

enter image description here

EDIT (ERROR AS TEXT):

Uncaught SyntaxError: Invalid regular expression: /(?<Month>[A-Za-z]{3}) (?<Date>[0-9]{2}) (?<Hour>[0-9]{2}):(?<Minutes>[0-9]{2}):(?<Seconds>[0-9]{2}) (?<ComputerName>[A-Za-z0-9\-\ ]+): (?<KernalTime>[0-9.\[\]\ ]+) (?<Message>[A-Za-z0-9\(\)\.\ \-\:]+)/: Invalid group

1

1 Answer 1

2

As far as I'm aware JavaScript does not support named groups:

Named capturing groups in JavaScript regex?

You can either convert to numeric-index groups by removing the <Angle-Bracket-Parts>

var re = /([A-Za-z]{3}) ([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2}) ([A-Za-z0-9\-\ ]+): ([0-9\.\[\]\ ]+) ([A-Za-z0-9\(\)\.\ \-\:]+)/gi

Which will mean that you can only access the captured information by numeric offsets:

var match = re.exec(myString);
console.log( match[1] ) // Nov 24

or use something like:

http://xregexp.com/

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.