Click to See Complete Forum and Search --> : editing a string


obviousunknown
07-25-2003, 05:41 AM
say i've got a string definded that has spaces in it. What's the simplist way to change each of the spaces into plus signs?

ie: "dave is very cool" would turn into "dave+is+very+cool"

thanks

Charles
07-25-2003, 05:55 AM
<script type="text/javascript">
<!--
alert('dave is very cool'.replace(/\s+/g, '+'))
// -->
</script>

And see http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/string.html#1194258.

obviousunknown
07-25-2003, 06:16 AM
for future reference, how would i do this with any character instead of a space?

Jeff Mott
07-25-2003, 06:17 AM
/\s+/gThis will also replace line breaks and tabs with plus signs. As well as convert groups of spaces (and tabs and line breaks) into a single plus instead of one plus for every white space char. This may or may not have been what the original poster had in mind. In case it wasn't, all you would need to do is change the part I quoted to/ /g

obviousunknown
07-25-2003, 06:21 AM
charles helped me with what i need, but i'll keep your time in mind.
And I assume 'dave is very cool'.replace("very", "not") would result "dave is not cool"

Charles
07-25-2003, 06:25 AM
That would be:

<script type="text/javascript">
<!--
alert('dave is very cool'.replace(/very/, 'not'))
// -->
</script>

And see http://devedge.netscape.com/library/manuals/2000/javascript/1.3/guide/regexp.html.

obviousunknown
07-25-2003, 06:37 AM
ahhh, it all makes sense now.
and thanks again charles. always around to help me out.