Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

string.replace syntax 1

Status
Not open for further replies.

peterpann

Programmer
Jun 19, 2007
63
GB
What is the syntax to replace several occurances of characters in a string that each start with "/" and end with "#", each occurance to be replaced by "XXX" ? What is the syntax if the occurances end with a newline ? How can I match a short word without getting matches to longer words that start with the same letters ? Thanks.
 
Hi

You did not gave us too many details. If I did not guessed some of the situations, repeat your questions with some sample data.
peterpann said:
What is the syntax to replace several occurances of characters in a string that each start with "/" and end with "#", each occurance to be replaced by "XXX" ?
Code:
[gray]# eliminates the delimiters[/gray]
'bla'.replace(/\/[^#]*#/g,'XXX')

[gray]# or[/gray]

[gray]# keeps the delimiters[/gray]
'bla'.replace(/\/[^#]*#/g,'/XXX#')

[gray]# or[/gray]

[gray]# keeps the delimiters, more robust, more expansive[/gray]
'bla'.replace(/(\/)[^#]*(#)/g,'$1XXX$2')
peterpann said:
What is the syntax if the occurances end with a newline ?
Code:
[gray]# eliminates the newlines[/gray]
'bla'.replace(/foo\n/g,'XXX')

[gray]# or[/gray]

[gray]# keeps the newlines[/gray]
'bla'.replace(/foo\n/g,'XXX\n')

[gray]# or[/gray]
[gray]# keeps the newlines, more robust, more expansive[/gray]
'bla'.replace(/foo([\r\n]{1,2})/g,'XXX$1')
peterpann said:
How can I match a short word without getting matches to longer words that start with the same letters ?
See the first code.

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top