What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Messenger Plus! for Live Messenger » Scripting » how do i replace this text?

Pages: (2): « First « 1 [ 2 ] Last »
how do i replace this text?
Author: Message:
markee
Veteran Member
*****

Avatar

Posts: 1622
Reputation: 50
36 / Male / Flag
Joined: Jan 2006
RE: RE: how do i replace this text?
quote:
Originally posted by Mattike
Use regular expressions! ;)
code:
var nMessage = Message.replace(/(^|\s|\n)teh($|\s|\n)/gi, "$1the$2");

Why use such a complex regular expression when you can do the same by this.

code:
var nMessage = Message.replace(/teh/gi, "the");

This will do exactly the same thing as what your regular expression did except it is smaller and less confusing for those that are new to regular expressions.
[Image: markee.png]
12-01-2006 04:25 PM
Profile PM Find Quote Report
TazDevil
Full Member
***

Avatar
sleep(0);

Posts: 359
Reputation: 11
41 / Male / Flag
Joined: May 2004
RE: how do i replace this text?
quote:
Originally posted by markee
Why use such a complex regular expression when you can do the same by this.

code:
var nMessage = Message.replace(/teh/gi, "the");

This will do exactly the same thing as what your regular expression did except it is smaller and less confusing for those that are new to regular expressions.

the point is i want to replace ONLY words not every occurance of "teh", as in other words, the spelling might be correct....

i thought \w was supposed to be used but since what Mattike gave me works i wont question his authority ;)

now i need to make an array of the strings and corrections and a for loop.. i think
Window Manager is discontinued, for WLM try the new,
Enhancer for contact list docking, auto-hide, hide taskbar button, remove button flashing and remember keyboard layout in conversation windows
12-01-2006 04:31 PM
Profile PM Web Find Quote Report
Matti
Elite Member
*****

Avatar
Script Developer and Helper

Posts: 1646
Reputation: 39
32 / Male / Flag
Joined: Apr 2004
RE: how do i replace this text?
quote:
Originally posted by markee
Why use such a complex regular expression when you can do the same by this.

code:
var nMessage = Message.replace(/teh/gi, "the");

This will do exactly the same thing as what your regular expression did except it is smaller and less confusing for those that are new to regular expressions.
You should've read his question twice:
quote:
Originally posted by TazDevil
and how can i check if it is the actual word and not part of the word

eg. teh is replaced by the but the function should not replace tehn with then

t-h combination is what i type wrong most of the times...
thanks
He wants to have "teh" replaced, but not "tehn". With your simpler expression, it'll replace all occurrences of "teh", including "tehn". With my somewhat complexer expression, it'll only replace when:
  • "teh" is at the begin of the string OR preceded by a space or newline AND
  • "teh" is at the end of the string OR proceeded by a space or newline.
When it matches, it'll also re-add the captured spaces and newline characters to keep the remaining string intact. ;)
Plus! Script Developer | Plus! Beta Tester | Creator of Countdown Live | Co-developer of Screenshot Sender 5

Found my post useful? Rate me!
12-01-2006 04:33 PM
Profile E-Mail PM Web Find Quote Report
TazDevil
Full Member
***

Avatar
sleep(0);

Posts: 359
Reputation: 11
41 / Male / Flag
Joined: May 2004
RE: how do i replace this text?
code:
    words_wrong = new Array("teh", "tehn");
    words_right = new Array("the", "then");
    for(i = 0; i < words_wrong.length; i++)
    {
        message = message.replace(/(^|\s|\n)+ words_wrong[i] +($|\s|\n)/gi, "$1"+words_right[i]+"$2");
    }
    return message;


so what is wrong here? how can i add a variable in the regexp expression...
Window Manager is discontinued, for WLM try the new,
Enhancer for contact list docking, auto-hide, hide taskbar button, remove button flashing and remember keyboard layout in conversation windows
12-01-2006 04:52 PM
Profile PM Web Find Quote Report
markee
Veteran Member
*****

Avatar

Posts: 1622
Reputation: 50
36 / Male / Flag
Joined: Jan 2006
RE: how do i replace this text?
I'm very sorry for my mistake, though you still shouldn't need the \n in them at least because this is part of both \s and $.

quote:
Originally posted by TazDevil
code:
    words_wrong = new Array("teh", "tehn");
    words_right = new Array("the", "then");
    for(i = 0; i < words_wrong.length; i++)
    {
        message = message.replace(/(^|\s|\n)+ words_wrong[i] +($|\s|\n)/gi, "$1"+words_right[i]+"$2");
    }
    return message;


so what is wrong here? how can i add a variable in the regexp expression...

There is no way that I have fount to be able to have a variable in a regular expression, I don't think it is possible (though I could be wrong).
[Image: markee.png]
12-01-2006 05:08 PM
Profile PM Find Quote Report
CookieRevised
Elite Member
*****

Avatar

Posts: 15517
Reputation: 173
– / Male / Flag
Joined: Jul 2003
Status: Away
RE: how do i replace this text?
PART 1
quote:
Originally posted by markee (context: replace tags with text)
code:
var Sup = /\(\!SM\)/gi;

Don't use the case-insensitive flag there, see "CookieRevised's reply to [SOLVED] How do I create a variable thing like (!VER)?".


-------------------------------------------------------------------------------------


PART 2
quote:
Originally posted by roflmao456
im just making a script right now that will change some words into this using a list. how can i do that .....????

a list of words like this:
hello
hi
whatever
hey

changed into:
sup
yo
w/e
hay
For starters, never reuse the original string to put your replacements in!

This will give you wrong strings in the end because there is always the chance that you replace an already replaced word back into something else.

eg: the big mistake you've made here: [pre-release] Letter Switcher... (and my reply to that).

Thus, do not do something along these lines:
quote:
Originally posted by markee
You can do this all in one long line
code:
string = string.replace(/hello/gi,"sup").replace(/hi/gi,"yo").replace(/whatever/gi,"w/e").replace(/hey/gi,"hay");
You can do these all individually and you can set the regular expressions as variables like I did before, but why bother if you only have a small list like this.
This will only work decent if all the strings you replace with are different than any of the strings you search for. If you can not be sure of that, never use the same variable to change and to set a string.


-------------------------------------------------------------------------------------


PART 3
quote:
Originally posted by markee
Unfortunately I don't think it is possible to use a for count and an array so you will have to keep changing the script itself (though if anyone can prove me wrong I will be appreciative).
It is perfectly possible.

The replace method's second parameter can be a function on its own. With this you can perfectly do this.

eg: How the Replace method actually works (instead of "text" as replacement string you can use a function as a replacement string. This is what makes Replace so powerfull):
CookieRevised's reply to [Question] Isn't string.replace supposed to replace all occurences ?
(^^ explains the replace function)
CookieRevised's reply to Problem with RegExp + Replace
(^^ shows an example for replace using a function as replace text)
CookieRevised's reply to Replace Colour Codes
(^^ shows an example for replace using a function as replace text)
CookieRevised's reply to [I help them] VB2JS
(^^ shows an example for replace using a function as replace text)


-------------------------------------------------------------------------------------


PART 4
quote:
Originally posted by TazDevil
quote:
Originally posted by Mattike
quote:
Originally posted by TazDevil
and how can i check if it is the actual word and not part of the word

eg. teh is replaced by the but the function should not replace tehn with then
Use regular expressions! ;)
code:
var nMessage = Message.replace(/(^|\s|\n)teh($|\s|\n)/gi, "$1the$2");

i thought \w was supposed to be used (instead of \s|\n) but since what Mattike gave me works i wont question his authority ;)
It will not work in some cases. eg: the above regular expression from Mattike will not replace "teh" in  "Hello teh, how are you teh?"

You should use a word boundery tag instead: \b

\b is a word boundery, which includes, but is not limited to, a space or a new line character.


See Scripting Documentation > Regular Expression Syntax.


-------------------------------------------------------------------------------------


PART 5
quote:
Originally posted by markee
quote:
Originally posted by TazDevil
code:
words_wrong = new Array("teh", "tehn");
words_right = new Array("the", "then");
for (i = 0; i < words_wrong.length; i++) {
    message = message.replace(/(^|\s|\n) + words_wrong[ i ] + ($|\s|\n)/gi, "$1"+words_right[ i ]+"$2");
}
return message;
so what is wrong here? how can i add a variable in the regexp expression...
I don't think it is possible (though I could be wrong).
It is again perfectly possible.

To answer TazDevil's question on what is wrong:

A regular expression is not a string. A string is enclosed in single or double quotion marks:
var s1 = "this is a string";
var s2 = 'this is a string too';

A regular expression is a special code pattern:
var r1 = /this is a regular expression/flags;

Everything between the slashes (/) is a pattern, not a string, and must be in the regular expression syntax.

If you want to convert a string to a regular expression pattern you need to either define the variable explicitly as a regular expression object:
var r2 = new RegExp("this is a regular expression", "flags")

Or use the compile method to compile a string into a regular expression object:
var r3 = new RegExp().compile("this is a regular expression", "flags");

Some methods and functions, like the Replace function, can have a _string_ expression or a regular expression _pattern_ and will reconize it as such and perform certain actions depending on what it is. But you can not mix a regular expression pattern and a string expression like you did (words_wrong[ i ] is a string expression).

In other words:
code:
var regExp = new RegExp("\\b" + oldsubstring + "\\b", "gi");
var newmessage = oldmessage.replace(regExp, newsubstring);
But note that even if you implemented this in your snippet, your code will not work properly either because of what I've said in PART 2; you must not use the same variable as you replaceable text and replaced text in a for() loop to avoid changing "this" to "that" and in the next iteration changing "that" back to "this".




This post was edited on 12-02-2006 at 02:19 AM by CookieRevised.
.-= A 'frrrrrrrituurrr' for Wacky =-.
12-01-2006 09:02 PM
Profile PM Find Quote Report
TazDevil
Full Member
***

Avatar
sleep(0);

Posts: 359
Reputation: 11
41 / Male / Flag
Joined: May 2004
RE: how do i replace this text?
thanks cookie nice post.
Window Manager is discontinued, for WLM try the new,
Enhancer for contact list docking, auto-hide, hide taskbar button, remove button flashing and remember keyboard layout in conversation windows
12-01-2006 09:44 PM
Profile PM Web Find Quote Report
Pages: (2): « First « 1 [ 2 ] Last »
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On