Click to See Complete Forum and Search --> : Javascript Replace Bracket
mjxnjx
May 27th, 2004, 03:26 PM
Hello Gurus!
How do I replace the open and close bracket characters in a string using client side javascript? ([ and ])
Example:
value = document.MyForm.MyField.value;
value.replace('[','OPENBRACKET');
document.MyForm.MyField.value = value;
Thanx
Dr. Script
May 27th, 2004, 04:45 PM
Actually, the javascript .replace() method is meant to replace regular expressions, as shown here:<script type="text/javascript">
var testStr = "[12[34[56[78[90]]]]]"
var testStr2 = testStr.replace(/[[]/g,"OPENBRACKET")
alert(testStr +"\n"+ testStr2)
</script>This will give you everything you need, using the replace prototype given in the code, to use regular expressions to replace the [ character with the string, "OPENBRACKET".
Dr Script
shikida
August 11th, 2010, 10:43 AM
what about closing bracket for firefox?
may be my ignorance, but
texto = texto.replace(/[[]/g,''); //works in both browsers
texto = texto.replace(/[]]/g,''); //works only in IE
TIA
Kenji
HanneSThEGreaT
August 12th, 2010, 08:24 AM
Shikida, this thread is 6 years old!
I know you are just trying to help fellow members here, but please revive threads with caution - don't make a habit of it
Hannes
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.