Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[JS] Variable-bound Case-insensitive global string replace?

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Trow
Grandmaster Cheater
Reputation: 2

Joined: 17 Aug 2006
Posts: 957

PostPosted: Mon Feb 08, 2010 10:51 am    Post subject: [JS] Variable-bound Case-insensitive global string replace? Reply with quote

Hi,

Normally, if you want to perform a string replacement in javascript, you do:
Code:
str=str.replace("what","lol");

but this is both case-sensitive and happens only for the first occurance of the term.

To do a case-insensitive global string replacement, there is regex:
Code:
str=str.replace(/what/gi,"lol");

and it works well.

Problem is, what should you do if you want a variable to be used inside the regex expression? The following are examples of things that don't work.
Code:
str=str.replace(/variablename/gi,"lol");
str=str.replace("/variablename/gi","lol");
str=str.replace("/" + variablename + "/gi","lol");

_________________
Get kidnapped often.
Back to top
View user's profile Send private message
shhac
Expert Cheater
Reputation: 0

Joined: 30 Oct 2007
Posts: 108

PostPosted: Mon Feb 08, 2010 1:10 pm    Post subject: Reply with quote

You can create regular expressions from strings (though usually it is better to use the /reg/ notation so only use this if the expression must be generated via string), for example,
Code:
re = new RegExp("foo"+variable+"bar","gi");
str = str.replace(re,"lol");
Remember to escape backslashes because this method uses a string and not direct typing of a RegEx.
Back to top
View user's profile Send private message
Trow
Grandmaster Cheater
Reputation: 2

Joined: 17 Aug 2006
Posts: 957

PostPosted: Mon Feb 08, 2010 5:19 pm    Post subject: Reply with quote

works like a charm. thanks!
_________________
Get kidnapped often.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites