![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
I am trying to write a routine which follows this logic, but not sure how to implement: script xxx = "... apple green yellow blue ..."; {right here I need to modify part of the string, so i.e. just 'green' in the string above would become 'purple''} alert(xxx); /script How can I do this? Thank you!! ) |
#3
| |||
| |||
|
|
amyx... (AT) gmail (DOT) com wrote: I am trying to write a routine which follows this logic, but not sure how to implement: script xxx = "... apple green yellow blue ..."; {right here I need to modify part of the string, so i.e. just 'green' in the string above would become 'purple''} alert(xxx); /script How can I do this? Thank you!! )Check out regular expressions on the net or get a book reference. Very valuable stuff. xxx = xxx.replace( /green/, 'purple' ); for global replacement ( i.e. if xxx == 'apple green yellow blue green' ) xxx = xxx.replace( /green/g, 'purple' ); for cas-insensitive replacement (i.e. xxx == 'apple Green yellow blue') xxx = xxx.replace( /green/i, 'purple' ) -b |
-A
#4
| |||
| |||
|
|
I am trying to write a routine which follows this logic, but not sure how to implement: script xxx = "... apple green yellow blue ..."; {right here I need to modify part of the string, so i.e. just 'green' in the string above would become 'purple''} alert(xxx); /script How can I do this? Thank you!! ) |
#5
| |||
| |||
|
|
bobzimuta wrote: Check out regular expressions on the net or get a book reference. Very valuable stuff. xxx = xxx.replace( /green/, 'purple' ); for global replacement ( i.e. if xxx == 'apple green yellow blue green' ) xxx = xxx.replace( /green/g, 'purple' ); for cas-insensitive replacement (i.e. xxx == 'apple Green yellow blue') xxx = xxx.replace( /green/i, 'purple' ) Actually, do you really modify the string this way? String is immutable in JavaScript isn't it? So it's not that the String is modified, rather, the new String that is returned by the call to replace() is assigned to xxx, is it? |
#6
| |||
| |||
|
|
Actually, do you really modify the string this way? String is immutable in JavaScript isn't it? So it's not that the String is modified, rather, the new String that is returned by the call to replace() is assigned to xxx, is it? |
#7
| |||
| |||
|
|
JRS: In article <1154458462.939815.289910 (AT) p79g2000cwp (DOT) googlegroups.com , dated Tue, 1 Aug 2006 11:54:23 remote, seen in news:comp.lang.javascript, amyxmas (AT) gmail (DOT) com posted : I am trying to write a routine which follows this logic, but not sure how to implement: script xxx = "... apple green yellow blue ..."; {right here I need to modify part of the string, so i.e. just 'green' in the string above would become 'purple''} alert(xxx); /script How can I do this? Thank you!! )Be aware that you cannot; a string cannot be modified. All that one can do is to generate a new string and maybe assign it to the variable xxx, which (if xxx is the only reference to the string) will free the original for garbage collection. Generally, but not invariably, the effects of unalterability are immaterial. To dal with the contents of a string positionally, use such as .indexOf and .substring; to deal more textually, including pattern-matching, consider using a RegExp : see <URL:http://www.merlyn.demon.co.uk/js- valid.htm> and its links. Read the newsgroup FAQ. |
#8
| |||
| |||
|
|
Ray wrote: Actually, do you really modify the string this way? String is immutable in JavaScript isn't it? So it's not that the String is modified, rather, the new String that is returned by the call to replace() is assigned to xxx, is it? More or less. Strings in JavaScript are immutable. |
![]() |
| Thread Tools | |
| Display Modes | |
| |