Solved

Replace non-alphanumeric characters by underscore.

Posted on 2001-06-28
13
3,035 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Javascript to set controls visibility 5 36
Problem to refer to value 8 50
how can i make the array into an object? 4 32
Passport Expiry 3 44
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…
I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
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…

810 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