Trooper78
Trooper78

Reputation: 31

Regular expression for string in nodejs

My string is : BETA-GE=a034-56aw-y567-54er; beta=/;

I would like to retrieve the string "a034-56aw-y567-54er" from the above string in nodejs.

The code I have written currently is:

var xs = "BETA-GE=a034-56aw-y567-54er; beta=/;";
var regex = /^BETA-GE=[a-zA-Z0-9-]*[;]*./;
let match = regex.exec(xs);
console.log(match[0]);

The result displays BETA-GE=a034-56aw-y567-54er;

Upvotes: 0

Views: 118

Answers (2)

Krophil
Krophil

Reputation: 21

You have just to create a group in your regexp and get the second match :

var xs = "BETA-GE=a034-56aw-y567-54er; beta=/;";
var regex = /^BETA-GE=([a-zA-Z0-9-]*)[;]*./;
let match = regex.exec(xs);
console.log(match[1]);

Upvotes: 0

Alon Adler
Alon Adler

Reputation: 4024

Surround the part you want with a capture group and access it:

var xs = "BETA-GE=a034-56aw-y567-54er; beta=/;";
var regex = /^BETA-GE=([a-zA-Z0-9-]*)[;]*./;
let match = regex.exec(xs);
console.log(match[1]);

Here is the working regex.

Upvotes: 3

Related Questions