Click to See Complete Forum and Search --> : Javascript Replace Bracket


mjxnjx
May 27th, 2004, 02: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, 03: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