[Home] [Downloads] [Search] [Help/forum]


Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Jscript
. . -> [Subject]  RegEx \w not working

RegEx \w not working

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


Posted by Kyrock   (20 posts)  [Biography] 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.
[Go to top] top

Posted by Twisol   USA  (2,257 posts)  [Biography] 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
[Go to top] top

Posted by Kyrock   (20 posts)  [Biography] 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
[Go to top] 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.


15,014 views.

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

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

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

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at HostDash]