Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 486
  • Last Modified:

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

0
hankknight
Asked:
hankknight
2 Solutions
 
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
 
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
informaniacCommented:
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
 
Roger BaklundCommented:
Try this:


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

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now