- 1). Launch an HTML editor or Notepad.
- 2). Open an HTML document and place this JavaScript code in its "head" section:
<script type="text/javascript">
function removeSlash(textString, replaceWith) {
var target = "/'";
var replacementString = "'";
var apostrophe = "'";
if (replaceWith != undefined) {
target = "/" + replaceWith;
replacementString = replaceWith;
}
var newString = testString.replace(new RegExp(target, 'g'), replacementString);
return newString;
}
var testString = "12/'34/'56/2";
var result = removeSlash(testString);
alert(result);
</script>
The "removeSlash" function accepts a text string as a parameter. It then creates a new "RegExp" object used to remove all slashes that appear before apostrophes. The three lines of code after the function allow you to test the function by passing it the value stored in the testString variable. - 3). Save your document and view it in a browser. The JavaScript function processes the string containing the slash before the apostrophe, removes the slash and displays the resulting string.
next post