Solved

Replace non-alphanumeric characters by underscore.

Posted on 2001-06-28
13
3,019 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
  • 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…

757 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now