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?

how do i replace this text?
Author: Message:
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
« Next Oldest Return to Top Next Newest »

Messages In This Thread
how do i replace this text? - by roflmao456 on 11-21-2006 at 02:55 AM
RE: how do i replace this text? - by markee on 11-21-2006 at 02:59 AM
RE: how do i replace this text? - by roflmao456 on 11-21-2006 at 02:59 AM
RE: how do i replace this text? - by markee on 11-21-2006 at 03:12 AM
RE: RE: how do i replace this text? - by roflmao456 on 11-21-2006 at 03:25 AM
RE: how do i replace this text? - by markee on 11-21-2006 at 03:37 AM
RE: how do i replace this text? - by roflmao456 on 11-21-2006 at 04:14 AM
RE: how do i replace this text? - by TazDevil on 12-01-2006 at 04:08 PM
RE: how do i replace this text? - by Matti on 12-01-2006 at 04:16 PM
RE: RE: how do i replace this text? - by markee on 12-01-2006 at 04:25 PM
RE: how do i replace this text? - by TazDevil on 12-01-2006 at 04:23 PM
RE: how do i replace this text? - by TazDevil on 12-01-2006 at 04:31 PM
RE: how do i replace this text? - by Matti on 12-01-2006 at 04:33 PM
RE: how do i replace this text? - by TazDevil on 12-01-2006 at 04:52 PM
RE: how do i replace this text? - by markee on 12-01-2006 at 05:08 PM
RE: how do i replace this text? - by CookieRevised on 12-01-2006 at 09:02 PM
RE: how do i replace this text? - by TazDevil on 12-01-2006 at 09:44 PM


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