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 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 code: RE: how do i replace this text? by roflmao456 on 11-21-2006 at 02:59 AM
ty il try that 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: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: 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: 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 RE: how do i replace this text? by Matti on 12-01-2006 at 04:16 PM
Use regular expressions! code: RE: how do i replace this text? by TazDevil on 12-01-2006 at 04:23 PM
quote: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:Why use such a complex regular expression when you can do the same by this. code: 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: 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:You should've read his question twice: quote: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:
RE: how do i replace this text? by TazDevil on 12-01-2006 at 04:52 PM
code: 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: 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: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: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: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: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: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: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: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. |