[RESOLVED]Trying to find and replace string by javascript

i was trying to find and replace string by JavaScript but my routine is not working. i guess there is some mistake. please guide me what to fix in js routine. here is my script.

function ReplaceChars(srcString)
        {
            var IgnoreChars = ['<', '>',':', '/', '?', '#', '[', ']', '@', '!', '$', '&', '(', ')', '*', '', '+', ',', ';', '=', ']', ';'];
            for (i = 0; i < srcString.length; i++) {
                for (j = 0; j < IgnoreChars.length; j++) {
                    if (srcString.charAt(i) == IgnoreChars.charAt(j))
                    {
                        srcString=srcString.replace(srcString.charAt(i), '');
                    }
                }
            }
            return srcString;
        }

        var str = '<name>';
        alert(ReplaceChars(str));

any way problem sorted.

<script type="text/javascript">
        function ReplaceChars(srcString)
        {
            var IgnoreChars = [‘%’,’<’, ‘>’,’:’, ‘/’, ‘?’, ‘#’, ‘[', ']‘, ‘@’, ‘!’, ‘$’, ‘&’, ‘(‘, ‘)’, ‘*’,’+’, ‘,’, ‘;’, ‘=’, ‘]’, ‘;’];
            for (i = 0; i < srcString.length; i++) {
                for (j = 0; j < IgnoreChars.length; j++) {
                    if (srcString.charAt(i) == IgnoreChars[j])
                    {
                        srcString=srcString.replace(srcString.charAt(i), ”);
                    }
                }
            }
            return srcString;
        }

        var str = ‘<name>’;
        alert(ReplaceChars(str));
    </script>

You should be able to use a Regular Expression to match these and replace them. It’s likely quite a bit cleaner than looping through all of your possible values :

// Build your clean-up function
function ReplaceChars(s){
     // Build your expression to strip out any of these characters
     return s.replace(/[\<>:/?#[]@!$&()*+,;=]/g,'');
}

You can see a working example of this here.

what is the meaning of these characters / &
/
g ??

mou_inn

what is the meaning of these characters / &
/g ??

The ‘/’ character is used as an escape character within regular expressions. It indicates that the expression will match that exact character as opposed to any other context that it might have (for instance the ‘.’ character is generally considered a universal
character and will match anything, however ‘/.’ will explicitly match a period). The trailing ‘/g’ that appears at the end is used within Regular Expressions in JavaScript to indicate a flag. The ‘g’ flag specifically indicates a "global" match and will match
any of the matching elements regardless of where they occur within the string.

 

@Rion: i always found your answer was great. i just post two new thread. i want you to have a look at and if possible give your idea.

this is my new threads url

http://forums.asp.net/t/2016982.aspx?How+to+implement+Https+and+SSL+for+asp+net+web+site

http://forums.asp.net/t/2016984.aspx?what+is+difference+among+the+library+framework+and+SDK+in+concept

thanks

Leave a Reply