Shoutbox

how do i replace this text? - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Messenger Plus! for Live Messenger (/forumdisplay.php?fid=4)
+---- Forum: Scripting (/forumdisplay.php?fid=39)
+----- Thread: how do i replace this text? (/showthread.php?tid=68703)

how do i replace this text? by roflmao456 on 11-21-2006 at 02:55 AM

ok i need help in how to replace this text like

EX. :

i type:   lol (!SM)

then it ends up when i send it:

John says: lol Sup Mate




how do i do that?


RE: how do i replace this text? by markee on 11-21-2006 at 02:59 AM

You can either use this code that I did (using regular expressions) or you could use something like answering machine+(I realise this wasn't the right thing :P)

code:
var Sup = /\(\!SM\)/gi;
function OnEvent_ChatWndSendMessage(wnd,msg){
    msg = msg.replace(Sup,"Sup Mate");
    return msg;
}


RE: how do i replace this text? by roflmao456 on 11-21-2006 at 02:59 AM

ty il try that

edit: is it possible to replace a normal strand of text like

i type: LoL this

to replace "this" in what i type

then it will turn up like

John says: LoL hello

lol im alittle noob on this "replacing stuff" but im good at everything else :P


RE: how do i replace this text? by markee on 11-21-2006 at 03:12 AM

I suggest you look up how to do regular expressions, they help outheaps.  For just a basic string (using just letters or numbers assymbols can have a different meaning) then you can use my code as anexample and put the likes of the following

code:
var replace1 = /text/gi;
function ...(...){
    string = string.replace(replace1,"text to replace it with");
}
For symbols like / \ . * $ ^ + ?  etc then you will need to prefix those characters with \

There are a heap more thing involved in regular expressions but those few things will get you started (though I suggest you try learning more).
RE: RE: how do i replace this text? by roflmao456 on 11-21-2006 at 03:25 AM

quote:
Originally posted by markee
I suggest you look up how to do regular expressions, they help outheaps.  For just a basic string (using just letters or numbers assymbols can have a different meaning) then you can use my code as anexample and put the likes of the following

code:
var replace1 = /text/gi;
function ...(...){
    string = string.replace(replace1,"text to replace it with");
}
For symbols like / \ . * $ ^ + ?  etc then you will need to prefix those characters with \

There are a heap more thing involved in regular expressions but those few things will get you started (though I suggest you try learning more).


it doesn't seem i can use that with OnEvent_ChatWndReceiveMessage or OnEvent_ChatWndSendMessage... it works now...

im just making a script right now that will change some words into this using a list. how can i do that .....????


EDIT: like say

a list of words like this:

hello
hi
whatever
hey

changed into

sup
yo
w/e
hay
RE: how do i replace this text? by markee on 11-21-2006 at 03:37 AM

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.  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).
RE: how do i replace this text? by roflmao456 on 11-21-2006 at 04:14 AM

thanks so much i hope this will help other people viewing this


RE: how do i replace this text? by TazDevil on 12-01-2006 at 04:08 PM

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


RE: how do i replace this text? by Matti on 12-01-2006 at 04:16 PM

Use regular expressions! ;)

code:
var nMessage = Message.replace(/(^|\s|\n)teh($|\s|\n)/gi, "$1the$2");

RE: how do i replace this text? by TazDevil on 12-01-2006 at 04:23 PM

quote:
Originally posted by Mattike
/(^|\s|\n)teh($|\s|\n)/gi, "$1the$2"
thanks,
you are a life saver, it would have taken ages to compile that string expression ;)

no more spelling mistakes in chat then :)
RE: RE: how do i replace this text? by markee on 12-01-2006 at 04:25 PM

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.
RE: how do i replace this text? by TazDevil on 12-01-2006 at 04:31 PM

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
RE: how do i replace this text? by Matti on 12-01-2006 at 04:33 PM

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. ;)
RE: how do i replace this text? by TazDevil on 12-01-2006 at 04:52 PM

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...
RE: how do i replace this text? by markee on 12-01-2006 at 05:08 PM

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).
RE: how do i replace this text? by CookieRevised on 12-01-2006 at 09:02 PM

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".




RE: how do i replace this text? by TazDevil on 12-01-2006 at 09:44 PM

thanks cookie nice post.