JavaScript/REGEX: "w" breaks regex

My REGEX does not work if the character "w" is found:

alert(getVal('zzz_','Hello zzz_TEST ABC '));
alert(getVal('zzz_','Hello zzz_Worldwide ABC '));
alert(getVal('zzz_','Hello zzz_Swine ABC '));

function getVal(prefix, str) {
 return String(new RegExp(prefix + "[^\w+) ]*").exec(str)).substr(prefix.length);
}

Open in new window

LVL 16
hankknightAsked:
Who is Participating?
 
Roger BaklundCommented:
Try this:


 function getVal(prefix, str) {
  return String(new RegExp(prefix + "[\\w]*").exec(str)).substr(prefix.length);
 }

Open in new window

0
 
käµfm³d 👽Commented:
Try doubling the backslash before the "\w":

...
 return String(new RegExp(prefix + "[^\\w+) ]*").exec(str)).substr(prefix.length);
}

Open in new window

0
 
ChetOS82Commented:
What exactly are you trying to do here?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
David S.Commented:
You can't use the "\w" shorthand character set inside a regular character set. Therefore it's not working because it was told not to match the "w" character.

Like Chet, it's unclear to me what you want the regular expression to match.
0
 
Obadiah ChristopherCommented:
return String(new RegExp(prefix + "[^\w+) ]*").exec(str)).substr(prefix.length);

Is it a typo or you are missing a (

return String(new RegExp(prefix + "[^(\w+) ]*").exec(str)).substr(prefix.length);
0
 
hankknightAuthor Commented:
Thanks, but none of ideas solve my problem.  

The problem is that instead of getting "Swine" I just get "S" because the w and everything after it is destroyed.

I have clarified what I need.  Please test this example.  It should return:
TEST
Worldwide
Swine

But it returns
TEST
World
S


<script type="text/javascript">

alert(getVal('zzz_','Hello zzz_TEST ABC '));  // Should return TEST
alert(getVal('zzz_','Hello zzz_Worldwide ABC ')); // Should return Worldwide
alert(getVal('zzz_','Hello zzz_Swine ABC ')); // Should return Swine

function getVal(prefix, str) {
 return String(new RegExp(prefix + "[^\w+) ]*").exec(str)).substr(prefix.length);
}

</script>
0
 
käµfm³d 👽Commented:
@Kravimir
You can't use the "\w" shorthand character set inside a regular character set.
Actually you can.

<!DOCTYPE html>
<html>
  <body>
    <script>
      var test = "hello world!";
      var reg = new RegExp("[\\w]+");
      var match = reg.exec(test);
      
      alert(match);
    </script>
  </body>
</html>

Open in new window


@hankknight
You are using a negated character class ( [^ ... ] ), which means you want to find anything that is NOT listed within the brackets. This means that "NOT \w" is anything not a letter, number, or underscore. Try using a capital "W"--effectively turning your code into a double-negation--instead:

...
 return String(new RegExp(prefix + "[^\\W+) ]*").exec(str)).substr(prefix.length);
}

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.