Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ Jscript ➜ RegEx \w not working

RegEx \w not working

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Kyrock   (20 posts)  Bio
Date Sun 14 Oct 2012 08:08 PM (UTC)
Message
So, their may already be a way of doing this, but I'm trying to make the toString methods of my objects return the constructor name. But, when I tried to use the javascript RegEx the \w (word character) doesn't work. So I create a alias called test with this script.


world.Note("testing");
var pattern = /\w/g;
var text = "this those that.";
var result = text.match(pattern);
if (result != null) {
  for (var i = 0;i < result.length;i++) {
    world.Note(i + ":" + result);
  }
}


This just outputs "testing". If I use the equivalent range set though:


world.Note("testing");
var pattern = /[a-zA-z0-9_]/g;
var text = "this those that.";
var result = text.match(pattern);
if (result != null) {
  for (var i = 0;i < result.length;i++) {
    world.Note(i + ":" + result);
  }
}

I get testing and each letter noted. I tried it in firefox and \w works fine.
Top

Posted by Twisol   USA  (2,257 posts)  Bio
Date Reply #1 on Sun 14 Oct 2012 08:16 PM (UTC)

Amended on Sun 14 Oct 2012 08:22 PM (UTC) by Twisol

Message
Are you editing from MUSHclient's alias, trigger, or timer dialogs? If so, you need to escape any backslashes, because MUSHclient pre-processes the text content before passing it on to the script engine.
world.Note("testing");
var pattern = /\\w/g;
var text = "this those that.";
var result = text.match(pattern);
if (result != null) {
  for (var i = 0;i < result.length;i++) {
    world.Note(i + ":" + result[i]);
  }
}


Because MUSHclient sees "\w", which is an invalid escape sequence, it replaces it with "w". There are no "w" characters in your test string, so nothing is matched. If you use "\\w" instead, MUSHclient will process that into "\w", then JScript will get ahold of it and properly identify it as the word character class.

[EDIT]: Hilariously, the forums do something similar, so when I want to show "\\w" I need to type "\\\\w".

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
Top

Posted by Kyrock   (20 posts)  Bio
Date Reply #2 on Sun 14 Oct 2012 08:27 PM (UTC)
Message
heh, that's a lot of slashes and yeah, that did the trick. Thanks
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


16,815 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.