René Nyffenegger's collection of things on the web
René Nyffenegger on Oracle - Most wanted - Feedback -
 

Search replace with JavaScript

'Ordinary' search replace

<html>
<head>
<script language='javascript'>

  function replace_i_with_a(s) {
    return s.replace('i','a');
  }

</script>

</head>
<body>

  <form>

     Enter text where <i>i</i>'s are to be replace by an <i>a</i>: <input name='txt' value='Mississippi'>
     <br><input type='button' value='Replace' onClick='javascript(txt.value=replace_i_with_a(txt.value))'>

  </form>


</body>
</html>

Search replace with a regular expression

<html>
<head>
<script language='javascript'>

  function replace_i_or_s_with_x(s) {
    return s.replace(/[is]/x/,'a');
  }

</script>

</head>
<body>

  <form>

     Enter text where <i>i</i>'s or <i>s</i>'es are to be replaced by an <i>x</i>: <input name='txt' value='Mississippi'>
     <br><input type='button' value='Replace' onClick='javascript(txt.value=replace_i_or_s_with_x (txt.value))'>

  </form>


</body>
</html>