Solved

Replace non-alphanumeric characters by underscore.

Posted on 2001-06-28
13
3,024 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Angular - why isn't this ng-if isn't working? 4 36
Open Attachment from within List Field in SharePoint Online 1 28
Split in Javascript 5 31
Change javascript css 1 19
In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…

910 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

20 Experts available now in Live!

Get 1:1 Help Now