# Javascript replace with expression

**URL:** <https://forum.kirupa.com/t/javascript-replace-with-expression/283684>\
**Category:** Uncategorized\
**Created:** [March 9, 2009, 11:22pm UTC](https://forum.kirupa.com/t/javascript-replace-with-expression/283684 "2009-03-09T23:22:20Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Patch](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/patch/32/1095_2.png) [@Patch](https://forum.kirupa.com/u/Patch)\
**Post date:** [March 9, 2009, 11:22pm UTC](https://forum.kirupa.com/t/javascript-replace-with-expression/283684/1 "2009-03-09T23:22:20Z")

</div>

Hi guys,

I’m working on a function that I want to remove speical charcters from. it currently looks like this:

```auto

var r={'special':/[\W]/g}
function iChars(obj,w){
  obj.value = obj.value.replace(r[w],'');
}

```

it works great, but I want to keep certain characters such as **’ " : \_ - ,** , basically quotation marks, colons and spaces. Where as my current script removes all special charcters. I know it can be done with php, but i’m working on something when it’s on a keyup/blur etc.

So is there a way or expression to make it so I can exclude certain characters from my expression? Or does someone else have anything they would recommend?

I’m new to character expressions, but any help would be greatly appeciated 🙂 Thanks.
