Problem with .replace method

Questions, tips and tricks and techniques for scripting in Servoy

Problem with .replace method

Postby Marco R. » Fri Jan 07, 2011 1:44 pm

Hi all,

I'm trying to use the ".replace" method into a string but only the first occurrence is replaced(smart client,developer)


Here an example:
test_lettersTemplate.servoy
(4.73 KiB) Downloaded 279 times



regards


Marco
Marco Rossi
Freelance

Main development environment: Servoy 6.1.6 - 7.4.3, Java 1.6u45,7u71 Windows 7/CentOS
Marco R.
 
Posts: 203
Joined: Thu Mar 19, 2009 12:37 pm

Re: Problem with .replace method

Postby Joas » Fri Jan 07, 2011 3:57 pm

I haven't looked at your solution, but you probably have to use the "g" flag in your regular expression. g for global:
Code: Select all
yourString.replace(/bla/g, "replacement");
Joas de Haan
Yield Software Development
Need help on your project? yieldsd.com
User avatar
Joas
Site Admin
 
Posts: 842
Joined: Mon Mar 20, 2006 4:07 pm
Location: Leusden, NL

Re: Problem with .replace method

Postby Marco R. » Fri Jan 07, 2011 6:31 pm

Sorry Joas I don't understand your answer. :(

I'm using 3 local string variable : "phrase1" "replacement" "phrase2"
Where into the "phrase1" I have my string template,into "replacement" I have the name of the user and "phrase2" is an empty string.

I do only:
Code: Select all
phrase2 = phrase1.replace("##mySpecialTag##",replacement)


Can you explain by using this example what I have to fix to make it work please?
Marco Rossi
Freelance

Main development environment: Servoy 6.1.6 - 7.4.3, Java 1.6u45,7u71 Windows 7/CentOS
Marco R.
 
Posts: 203
Joined: Thu Mar 19, 2009 12:37 pm

Re: Problem with .replace method

Postby rioba » Sat Jan 08, 2011 10:15 am

In your example you put the text to be replaced between quotation marks. This should in fact be a regular expression that should be put between two slashes (/regexp/), not a string as in your example. Moreover, if you don't add the "g" flag after the closing slash, the replace function will work only on the first occurrence of the pattern to be replaced. The replacement pattern must always be a string or a variable containing a string.

In my opinion, if you need to replace a variable your code should be something like this
Code: Select all
var myVar = mySpecialTag;
var myregExp = new RegExp(myVar,"g");
phrase2 = phrase1.replace(myregExp,"replacement string");

If you used a string as replace pattern instead of a variable you should try this (where mySpecialTag must be a string constructed as a a regular expression)
Code: Select all
phrase2 = phrase1.replace(/mySpecialTag/g,"replacement string");

Please note the "g" flag used as the second argument in the RegExp object construction or after the closing slash in the second example. This tells the replace function to check all the occurrences of the replace pattern
rioba
 
Posts: 242
Joined: Mon Aug 08, 2005 4:26 pm

Re: Problem with .replace method

Postby Joas » Mon Jan 10, 2011 10:14 am

For a little more information about regular expressions in javascript, here is an easy tutorial.
Joas de Haan
Yield Software Development
Need help on your project? yieldsd.com
User avatar
Joas
Site Admin
 
Posts: 842
Joined: Mon Mar 20, 2006 4:07 pm
Location: Leusden, NL

Re: Problem with .replace method

Postby Marco R. » Mon Jan 10, 2011 10:46 am

Joas and Rioba,thanks to both for your time and for your help.

Now the concept is clear,and all works fine. :)


Marco
Marco Rossi
Freelance

Main development environment: Servoy 6.1.6 - 7.4.3, Java 1.6u45,7u71 Windows 7/CentOS
Marco R.
 
Posts: 203
Joined: Thu Mar 19, 2009 12:37 pm

Re: Problem with .replace method

Postby jgarfield » Mon Jan 10, 2011 5:51 pm

For simple replaces, you might also be interested in trying out

phrase2 = utils.stringReplace(phrase1, "##mySpecialTag##', replacement)
Programmer.
adBlocks
http://www.adblocks.com
jgarfield
 
Posts: 223
Joined: Wed Sep 28, 2005 9:02 pm
Location: Boston, US

Re: Problem with .replace method

Postby Marco R. » Wed Jan 12, 2011 11:20 am

thanks jgarfield!
Marco Rossi
Freelance

Main development environment: Servoy 6.1.6 - 7.4.3, Java 1.6u45,7u71 Windows 7/CentOS
Marco R.
 
Posts: 203
Joined: Thu Mar 19, 2009 12:37 pm


Return to Methods

Who is online

Users browsing this forum: No registered users and 7 guests

cron