0

I am trying below code to get the output 1. But not able to get it.

var labhost = '0';
var podmaster_node = "evhlab65.ms.com";
var matchtxt = new RegExp('\w+\.ms\.com', "i");


if (podmaster_node.match(matchtxt)) {
    labhost = '1';
}

alert(labhost);

Please help me on this.

Thanks!

1
  • What exactly should the regex match? Commented Nov 4, 2014 at 8:08

1 Answer 1

2

When you are escaping Regular Expression in RegExp constructor, you need to escape like this

var matchtxt = new RegExp('\\w+\\.ms\\.com', "i");

\w+, translates to w+. So you need to escape \ with \\, which makes \ as part of the regular expression.

var labhost = '0';
var podmaster_node = "evhlab65.ms.com";
var matchtxt = new RegExp('\\w+\\.ms\\.com', "i");


if (podmaster_node.match(matchtxt)) {
  labhost = '1';
}

document.getElementById("result").innerHTML = labhost;
<div id="result" />

Note: The better way to do this would be to use Regular Expression literal, like this

var matchtxt = /\w+\.ms\.com/i;
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.