Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

regex to filter high bit characters 1

Status
Not open for further replies.

dougcranston

Technical User
Oct 5, 2001
326
US
I have a need to replace all high bit codes from content gathered via a form, prior to writing it to a database. Server is Windows 2000 Server, and I do most of my server and client side in Javascript/Jscript.

What I am shooting for is after running a regex replace all other characters EXCEPT FOR:

Dec 09
Dec 10
Dec 13
Dec 32 - 126

All others I want to replace with a "blank" or Dec 32

Is their an easy way to accomplish this in Javascript Regex?

I have searched the web but I suspect I am either using the wrong search terms or it just never has come up, which I would find hard to believe.

Thanks in advance.

DougCranston
 
I don't know... Bruteforce is something like this.
[tt]
function pchar_reduction(s) {
var p;
p="\n"+"\t"+"\r";
for (var i=32;i<127;i++) {
p+=(i==91||i==92|i==93||i==94)?"\\"+String.fromCharCode(i):String.fromCharCode(i);
}
p="["+p+"]"; //building pattern completed

var re=new RegExp(p,"g");
return s.match(re).join("");
}
[/tt]
- tsuji
 
Dan,

Yes.. What I want to retain is "normal" alpha numeric chars along with the \t \r and \n.. And replace any other special or high bit characters with a blank.

I am convinced that the problem is related to these "other" characters choking the app when it tries to load to the database. I suspect that some of my users have been cutting text from Word or another source but have not been able to confirm or prove it conclusively.

Thanks,

DougCranston
 
tsuji,

It looks like you have created/provided the solution I have been looking for.

I was afraid I was going to have to create 130+ individual replace statements and from what I think the code you have provided, does that in just a few lines.

Will try that here in a few minutes.

Thanks so much for taking the time to respond and with a solution that looks like it will solve my problem.

Thanks for your input.

DougCranston
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top