Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8165409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:31:47+00:00 2026-06-06T19:31:47+00:00

Why isn’t my code around the test function in Object RegExp working the same

  • 0

Why isn’t my code around the test function in Object RegExp working the same way as the regex pattern. Am I missing something or Am I using the wrong escape regex

<html>
<body>

<script type="text/javascript">

var str = "info@test.com";

//This isn't working
var regStr = "^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$"; //This string can be any regex get from XSLT

//Escape function get from: http://stackoverflow.com/a/6969486/193850
regStr = regStr.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
console.log(regStr); // \^\(\[w\-\]\+\(\?:\.\[w\-\]\+\)\*\)@\(\(\?:\[w\-\]\+\.\)\*w\[w\-\]\{0,66\}\)\.\(\[a\-z\]\{2,6\}\(\?:\.\[a\-z\]\{2\}\)\?\)\$
var re = new RegExp(regStr , "i");
console.log(re.test(str)); //false


var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
console.log(filter.test(str)); //true

</script>

</body>
</html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T19:31:48+00:00Added an answer on June 6, 2026 at 7:31 pm

    See, there’s a confusion. The function you’ve used is a nice way of preprocessing your string-to-be regexes so you don’t have to worry about escaping regex metacharacters – i.e., symbols that will control the regex behaviour, and not just taken literally.

    But the point is that your string has already been parsed before it was taken by this escaping function: \w and \. sequences became just w and . respectively, the preceding slash was lost.

    For characters not listed in Table 2.1, a preceding backslash is
    ignored, but this usage is deprecated and should be avoided.

    The escaper function, actually, did restore the slash before the ., but w wasn’t special for it in any kind. ) Therefore the string that went into RegExp constructor had [w] instead of [\w].

    It’s actually quite easy to check: just console.log(regStr) after the replacement operation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why isn't the following piece of code working in IE8? <select> <option onclick=javascript: alert('test');>5</option>
Isn't there a CSS way of having the page to get a border around
Isn't there some way to re-write the following code, such that I don't need
This isn't working the way I expect it to. Sample input will include an
Isn't there a Design Pattern who describes how to high cohesion? I need some
Isn't there a better way to accomplish this?: (RegionID is a Flex ComboBox) RegionID.selectedItem=value.Region;
Isn't factory a general singleton? Or may the Factory pattern be Singleton sometimes? Let's
Isn't this a simpler as well as safe (and hence better) way to implement
Isn't there a better looking statement (or way) to keep the console from disappearing
% isn't defined. modulo only works on integers. I want something equivalent to Javascript's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.