Javascript Replace Globally


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Javascript Replace Globally
# 1  
Javascript Replace Globally

I have a bit of javascript


function hasSpaces(name)
{
if(name.search(/^\s*/) != -1){
alert(name);
name.replace((/^\s*/, "~"))
alert(name);
}
return name;
}

The name comes in like john smith and i have to turn it to john~smith, the alerts are just for testing. Sorry if this is the wrong forum but i need help and am terrible at javascript. It also has to replace all spaces in the name in case the name is john adams smith to john~adams~smith

thanks
Sean

Last edited by insania; 06-27-2007 at 06:08 PM..
# 2  
Insania,
We know about unix -- Java in not in the plate.
If you are lucky, you will find somebody here with Java experience.
Good luck!
# 3  
Quote:
Originally Posted by Shell_Life
If you are lucky, you will find somebody here with Java experience.
Javascript is not Java.

The following example shows how to perform a global replace of whitespace character to "~".

Code:
var spx = /\s/g;
alert("adam smith king".replace(spx, "~"));

See:
http://developer.mozilla.org/en/docs...Objects:RegExp
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #985
Difficulty: Medium
386BSD and FreeBSD were both derived from BSD releases.
True or False?

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to call variable inside a function globally?

Hi Gurus, Is there a way to call a variable inside a function anywhere within the script? Thanks. BR, Ernesto (2 Replies)
Discussion started by: ernesto
2 Replies

2. UNIX for Dummies Questions & Answers

replacing a particular instance in a string globally

Hi, I'm trying to update the last two characters coming in a string globally in a file. Here is the sample data: file1 In file1, I want to have all instances replace where _o is appearing in the end of a word with _g. If _o is appearing in the middle or any other position except the... (2 Replies)
Discussion started by: er_ashu
2 Replies

3. Shell Programming and Scripting

renaming files globally

I am attempting to use a shell script to append the string "AAA" to the beginning of over 400 files and I am just missing that little piece to finish. I am still a newbie and I am still learning scripting. This is what I have so far: #!/usr/bin/ksh find /home/vedder191/AAA_revised -type... (4 Replies)
Discussion started by: vedder191
4 Replies

4. Shell Programming and Scripting

How to declare a variable which can be accessed globally

Hi I've few shell scripts which are responsible for triggering the continuous builds for a specific module. Each shell script is for a Module. Shell script has some module specific settings in the beginning and then it triggers the builds (which are nothing but some combination of Java programs... (2 Replies)
Discussion started by: kgsrinivas
2 Replies

5. Shell Programming and Scripting

Sed command to globally replace xth occurance.

This should replace the 1st and the 3rd occurance of "the": sed -e "s/ the / those /1;s/ the / these /3" This works only by line. If there is a second "the", but in an other line for example, it counts it as 1st again and replaces it again. How can I replace "the" 1st and 3rd... (14 Replies)
Discussion started by: lowmaster
14 Replies

6. Shell Programming and Scripting

Help with Find/Replace Javascript Injected Strings in mulitple files

Hi, guys, I'm not a high-end programmer, but I've been trying to write a script to remove all of the b.rtbn2.cn (and b.adserv.cn and any future variation) injected script tags on the server. (Still working on security fixes to prevent it in the future, just need to clean up now.) My approach is... (1 Reply)
Discussion started by: zzlegs
1 Replies

7. Shell Programming and Scripting

Redirect stdout/stderr to a file globally

Hi I am not if this is possible: is it possible in bach (or another shell) to redirect GLOBALLY the stdout/stderr channels to a file. So, if I have a script script.sh cmd1 cmd2 cmd3 I want all stdout/stderr goes to a file. I know I can do: ./script.sh 1>file 2>&1 OR ... (2 Replies)
Discussion started by: islegmar
2 Replies

8. Solaris

How to globally change root password?

I am the new admin of about 50 or so Solaris systems (both 8 & 9) that I need to change the root (&shadow) password to after the prior admin left. Is there a tool that can be used or a script that you like to use that can be used to perform a global change to prevent having to login to each... (2 Replies)
Discussion started by: sconewb
2 Replies

9. UNIX for Dummies Questions & Answers

installing globally

where is the best location in a system to install software globally for users to use. asuming your the sys admin. i know it might not matter, but just wondering. (2 Replies)
Discussion started by: savage
2 Replies

Featured Tech Videos