Solved

Replace non-alphanumeric characters by underscore.

Posted on 2001-06-28
13
3,064 Views
Last Modified: 2007-11-27
My goal is to replace all non-alphanumeric characters by '_'.  The following is the code I had.  At the end, when I display the final string, it was empty.  Please help me.  Thanks in advance.

==================================================
var str = "a b.3-4+";     // final string should be "a_b_3_4_"
var choice = "";

for ( i = 0; i < str.length; i++ )
    if ( str.charAt( i ) == ' ' || str.charAt( i ) == '-' ||
            str.charAt( i ) == '+' || str.charAt( i ) == '=' ||
            str.charAt( i ) == '*' || str.charAt( i ) == '&' ||
            str.charAt( i ) == '%' || str.charAt( i ) == '$' ||
            str.charAt( i ) == '@' || str.charAt( i ) == '#' ||
            str.charAt( i ) == ',' || str.charAt( i ) == '.' ||
            str.charAt( i ) == '>' || str.charAt( i ) == '<' )
        choice.concat( "_" );
    else
        choice.concat( str.charAt( i ));

alert( choice );
=========================================

Is there a better way to achieve what I wanted?

Thanks experts.
0
Comment
Question by:szetoa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 2
  • +2
13 Comments
 
LVL 7

Expert Comment

by:daniel_c
ID: 6235845
listening...
0
 
LVL 15

Expert Comment

by:a.marsh
ID: 6235886
First of all here is a much more efficient solution:

<html>
<head>
<script language="javascript">
<!--

function convertName(val, resultObj){
  var test = new RegExp("[^0-9,a-z,A-Z]", "g");

  resultObj.value = val.replace(test, "_");
}

//-->
</script>
</head>
<body>
<form>
<input type="text" name="str1" value="">
<p><input type="button" value="convert..." onClick="convertName(this.form.str1.value, this.form.str2);"></p>
<input type="text" name="str2" value="">
</form>
</body>
</html>

Ant
0
 
LVL 15

Expert Comment

by:a.marsh
ID: 6235891
And here is the working version of your code:

<html>
<head>
<script language="javascript">
<!--

var str = "a b.3-4+";     // final string should be "a_b_3_4_"
var choice = "";

for ( i = 0; i < str.length; i++ )
   if ( str.charAt( i ) == ' ' || str.charAt( i ) == '-' ||
           str.charAt( i ) == '+' || str.charAt( i ) == '=' ||
           str.charAt( i ) == '*' || str.charAt( i ) == '&' ||
           str.charAt( i ) == '%' || str.charAt( i ) == '$' ||
           str.charAt( i ) == '@' || str.charAt( i ) == '#' ||
           str.charAt( i ) == ',' || str.charAt( i ) == '.' ||
           str.charAt( i ) == '>' || str.charAt( i ) == '<' )
       choice += "_";
   else
       choice += str.charAt(i);

alert( choice );

//-->
</script>
</head>
<body>
</body>
</html>
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 12

Accepted Solution

by:
ahosang earned 0 total points
ID: 6235904
use regular expressions:
<html>
<head>
     <title>Untitled</title>
<script>
function replaceChars(textField) {
  var nonAlphaNum=/[^0-9,a-z,A-Z]/gi;
  textField.value=textField.value.replace(nonAlphaNum, "_");
}
</script>
</head>

<body>

test<br>
<form>
<input name="text1" type="text" value="">
<input type="button" value="check it" onclick="replaceChars(this.form.text1)">
</form>

</body>
</html>
0
 
LVL 12

Expert Comment

by:ahosang
ID: 6235923
Dang Ant, you're pretty sharp mister :-)
0
 
LVL 15

Expert Comment

by:a.marsh
ID: 6235936
;o)

Ant
0
 
LVL 1

Author Comment

by:szetoa
ID: 6235948
Thank you both Ant and ahosang.  The regular expression solution works like a charm.  Since Ant gave me the answer first, I am going to award the points to him.  Thanks again.
0
 
LVL 15

Expert Comment

by:a.marsh
ID: 6235955
Think you made a bit of a mistake there then szetoa......the button is at the top of the comment that you want to accept......

Be careful in the future.... :o)

I shall get Community Support to resolve this.

:o)

Ant
0
 
LVL 15

Expert Comment

by:a.marsh
ID: 6235960
0
 
LVL 1

Author Comment

by:szetoa
ID: 6235978
Thank you both Ant and ahosang.  The regular expression solution works like a charm.  Since Ant gave me the answer first, I am going to award the points to him.  Thanks again.
0
 
LVL 1

Author Comment

by:szetoa
ID: 6235992
Sorry Ant,  I am a little confused about the button. I just posted another question with the same amount of points just for you.
0
 
LVL 1

Expert Comment

by:Computer101
ID: 6236601
Points refunded so that they can be used in the question posted by szetoa for A.marsh.

Thank you
Computer101
Community Support Moderator
0
 
LVL 15

Expert Comment

by:a.marsh
ID: 6236930
Just to try to keep things all tied together, points awarded here:

http://www.experts-exchange.com/jsp/qShow.jsp?qid=20143064

:o)

Ant
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question