Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #23606 > unrolled thread

replacing characters in string

Started byjyklforum608@gmail.com
First post2014-03-15 17:02 -0700
Last post2014-03-16 10:44 +0100
Articles 3 — 3 participants

Back to article view | Back to comp.lang.javascript


Contents

  replacing characters in string jyklforum608@gmail.com - 2014-03-15 17:02 -0700
    Re: replacing characters in string Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-03-16 00:25 +0000
    Re: replacing characters in string "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-03-16 10:44 +0100

#23606 — replacing characters in string

Fromjyklforum608@gmail.com
Date2014-03-15 17:02 -0700
Subjectreplacing characters in string
Message-ID<410abe8b-ddb4-431c-9fbe-886cef41c689@googlegroups.com>
Hello

I have a string ( including double quotes ) like this: "hello world".

I'd like to get rid of all double quotes inside my string. I tried this code:

var myString = \"Hello world\";
myString = myString.replace('\"', '');

The above code works, but only replaces the first ", then it returns, so the output the I get is hello world".

How can I replace all the double quotes inside a string?

Thanks

PS: is the String.replace( r ) function meant to return/exit whenever it replaces the first instance of r?

[toc] | [next] | [standalone]


#23607

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2014-03-16 00:25 +0000
Message-ID<0.9cbf25889ee825c78c23.20140316002549GMT.87y50b3s6q.fsf@bsb.me.uk>
In reply to#23606
jyklforum608@gmail.com writes:

> I have a string ( including double quotes ) like this: "hello world".
>
> I'd like to get rid of all double quotes inside my string. I tried
> this code:
>
> var myString = \"Hello world\";
> myString = myString.replace('\"', '');
>
> The above code works,

I don't think so.  The first line should be

  var myString = "\"Hello world\"";

or, simper,

  var myString = '"Hello world"';

> but only replaces the first ", then it returns,
> so the output the I get is hello world".
>
> How can I replace all the double quotes inside a string?

Use a RegExp with the global flag:

  myString.replace(/"/g, '');

> PS: is the String.replace( r ) function meant to return/exit whenever
> it replaces the first instance of r?

Yes.

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#23608

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2014-03-16 10:44 +0100
Message-ID<XnsA2F26D52242C9eejj99@194.109.133.133>
In reply to#23606
jyklforum608@gmail.com wrote on 16 mrt 2014 in comp.lang.javascript:

> PS: is the String.replace( r ) function meant to return/exit whenever it
> replaces the first instance of r? 

No.

1
if you mean a regular expression with "r"
you should specify with what it is replaced with.

var resultString = sourceString.replace(regExpression, withString);

2
the functions returns a new string with the result, 
the original string being unaltered. 
So, if you want the originals string altered, 
you should assign the result to that same variable:


================================
<script type='text/javascript'>

var myString = 'abcabc';
myString = myString.replace(/a/,'X');
document.write(myString); // shows: Xbcabc

document.write('<br>');

var myString2 = 'abcabc';
myString2 = myString2.replace(/a/g,'Y');
document.write(myString2); // shows: YbcYbc

document.write('<br>');

var myString3 = 'abcAbc';
myString3 = myString3.replace(/a/g,'Z');
document.write(myString3); // shows: ZbcAbc

document.write('<br>');

var myString4 = 'abcAbc';
myString4 = myString4.replace(/a/gi,'Z');
document.write(myString4); // shows: ZbcZbc

</script>
================================




-- 
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