Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #37126 > unrolled thread
| Started by | Martin Leese <please@see.Web.for.e-mail.INVALID> |
|---|---|
| First post | 2019-08-13 14:18 -0600 |
| Last post | 2019-08-15 13:55 +0200 |
| Articles | 6 — 3 participants |
Back to article view | Back to comp.lang.javascript
string.replace() Martin Leese <please@see.Web.for.e-mail.INVALID> - 2019-08-13 14:18 -0600
Re: string.replace() dr.j.r.stockton@gmail.com - 2019-08-13 14:52 -0700
Re: string.replace() Martin Leese <please@see.Web.for.e-mail.INVALID> - 2019-08-14 15:36 -0600
Re: string.replace() "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2019-08-14 10:18 +0200
Re: string.replace() Martin Leese <please@see.Web.for.e-mail.INVALID> - 2019-08-14 15:41 -0600
Re: string.replace() "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2019-08-15 13:55 +0200
| From | Martin Leese <please@see.Web.for.e-mail.INVALID> |
|---|---|
| Date | 2019-08-13 14:18 -0600 |
| Subject | string.replace() |
| Message-ID | <qiv5ub$i5$1@dont-email.me> |
I nice easy one to start the day.
I wish to replace spaces in a string with
"+" characters. I am using:
var mungedTopic;
mungedTopic.replace(/ /g, "+");
but the spaces are not replaced. There can
be multiple spaces, and I want to replace
them all. I therefore need to use a regular
expression rather than a string. I guess my
regular expression is wrong, but I don't see
why.
--
Regards,
Martin Leese
E-mail: please@see.Web.for.e-mail.INVALID
Web: http://members.tripod.com/martin_leese/
[toc] | [next] | [standalone]
| From | dr.j.r.stockton@gmail.com |
|---|---|
| Date | 2019-08-13 14:52 -0700 |
| Message-ID | <dc4e4d48-dec0-4588-a8d6-1a25247e1f8c@googlegroups.com> |
| In reply to | #37126 |
On Tuesday, 13 August 2019 21:18:26 UTC+1, Martin Leese wrote: > I nice easy one to start the day. > > I wish to replace spaces in a string with > "+" characters. I am using: > var mungedTopic; > mungedTopic.replace(/ /g, "+"); > > but the spaces are not replaced. There can > be multiple spaces, and I want to replace > them all. I therefore need to use a regular > expression rather than a string. I guess my > regular expression is wrong, but I don't see > why. Method replace does not alter the pre-existing string, but returns an altered copy of it. So : mungedTopic = mungedTopic.replace(/ /g, "+"); -- (c) John Stockton, near London, UK. Using Google Groups. | Mail: J.R.""""""""@physics.org - or as Reply-To, if any. |
[toc] | [prev] | [next] | [standalone]
| From | Martin Leese <please@see.Web.for.e-mail.INVALID> |
|---|---|
| Date | 2019-08-14 15:36 -0600 |
| Message-ID | <qj1ut3$2u6$1@dont-email.me> |
| In reply to | #37128 |
dr.j.r.stockton@gmail.com wrote: > On Tuesday, 13 August 2019 21:18:26 UTC+1, Martin Leese wrote: >> I nice easy one to start the day. >> >> I wish to replace spaces in a string with >> "+" characters. I am using: >> var mungedTopic; >> mungedTopic.replace(/ /g, "+"); >> >> but the spaces are not replaced. There can >> be multiple spaces, and I want to replace >> them all. I therefore need to use a regular >> expression rather than a string. I guess my >> regular expression is wrong, but I don't see >> why. > > Method replace does not alter the pre-existing string, but returns an altered copy of it. > > So : mungedTopic = mungedTopic.replace(/ /g, "+"); Many thanks, that looks like it will work. Can't test it at the moment; it is part of an AJAX system, and the whole thing is down. -- Regards, Martin Leese E-mail: please@see.Web.for.e-mail.INVALID Web: http://members.tripod.com/martin_leese/
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2019-08-14 10:18 +0200 |
| Message-ID | <XnsAAAB68E927159eejj99@194.109.6.166> |
| In reply to | #37126 |
ram@zedat.fu-berlin.de (Stefan Ram) wrote on 14 Aug 2019 in comp.lang.javascript: > Martin Leese <please@see.Web.for.e-mail.INVALID> writes: >>mungedTopic.replace(/ /g, "+"); >>but the spaces are not replaced. > > main.js > > print( "True 1 1 0".replace( / /g, "+" )); > > transcript > > True+1+1+0 However, since there are other characters that produce a visible space, like the tab charater for instance, you possibly could prefer to use the \s for 'any spacelike character': myString = myString.replace( /\s/g, '+' )); ========================== If you are unsure about using Regex, replace() can also, in a simple way, be used without that: myString = myString.replace( ' ', '+' )); but then the /g for 'global' would not be there, and only the first instance of a space would be replaced. -- Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
| From | Martin Leese <please@see.Web.for.e-mail.INVALID> |
|---|---|
| Date | 2019-08-14 15:41 -0600 |
| Message-ID | <qj1v5t$4q0$1@dont-email.me> |
| In reply to | #37132 |
Evertjan. wrote: > However, > since there are other characters that produce a visible space, > like the tab charater for instance, > you possibly could prefer to use the \s for 'any spacelike character': > > myString = myString.replace( /\s/g, '+' )); The strings are mine (as opposed to user input) so either will do. This suggestion is more general, and so more bullet proof, so I will use it. > If you are unsure about using Regex, > replace() can also, in a simple way, be used without that: > > myString = myString.replace( ' ', '+' )); > > but then the /g for 'global' would not be there, > and only the first instance of a space would be replaced. My original post explained that there can be more than one space, so I have to use a regular expression. Thanks for your help. -- Regards, Martin Leese E-mail: please@see.Web.for.e-mail.INVALID Web: http://members.tripod.com/martin_leese/
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2019-08-15 13:55 +0200 |
| Message-ID | <XnsAAAC8DB5EBF18eejj99@194.109.6.166> |
| In reply to | #37141 |
Martin Leese <please@see.Web.for.e-mail.INVALID> wrote on 14 Aug 2019 in comp.lang.javascript: > Evertjan. wrote: >> However, >> since there are other characters that produce a visible space, >> like the tab charater for instance, >> you possibly could prefer to use the \s for 'any spacelike character': >> >> myString = myString.replace( /\s/g, '+' )); > > The strings are mine (as opposed to user > input) so either will do. This suggestion > is more general, and so more bullet proof, > so I will use it. > >> If you are unsure about using Regex, >> replace() can also, in a simple way, be used without that: >> >> myString = myString.replace( ' ', '+' )); >> >> but then the /g for 'global' would not be there, >> and only the first instance of a space would be replaced. > > My original post explained that there can > be more than one space, so I have to use a > regular expression. This is Usenet, not a helpdesk, so responses go to the group, and may exeed an original question. > Thanks for your help. It may be expedient for you to learn Regex syntax and methods, and not depend on thes same of others. -- Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web