admin管理员组文章数量:1290984
I'm looking for a javascript function which takes a string parameter and checks for ascii characters lower than 32, replacing them with empty string -> "". I'm new to javascript so I was wondering whether anyone can point me towards the right direction ?
Thanks in advance for your time.
I'm looking for a javascript function which takes a string parameter and checks for ascii characters lower than 32, replacing them with empty string -> "". I'm new to javascript so I was wondering whether anyone can point me towards the right direction ?
Thanks in advance for your time.
Share Improve this question asked Nov 15, 2012 at 13:24 PumpkinPumpkin 2,0433 gold badges27 silver badges33 bronze badges 3- Also, why? Might be a better way. – Prof. Falken Commented Nov 15, 2012 at 13:26
- stackoverflow./questions/94037/… this may help – Ferhat Commented Nov 15, 2012 at 13:27
- What I meant was, I want to find and remove any characters within the string that has a lower ascii value than 32 – Pumpkin Commented Nov 15, 2012 at 13:28
3 Answers
Reset to default 7Try this:
var replaced = string.replaceAll("[^ -~]", "");
Using ^
negates the characters class, and since space is character 32 in the ASCII table and ~ is the last printable character, you're basically saying "everything that isn't a printable character".
To simply remove all characters from 0-31 use:
var replace = string.replaceAll("\x00-\x1F", "");
If I understand your question correctly you are looking for a regex to use with .replace...
For replacing any printable ascii chars you can use this regex:
/[ -~]/
You will probably have to adjust the range. I remend changing the tilder since it is the last printable char.
Sorry, I see what you mean! I think you cannot match unprintable chars unless use use their special symbol: i.e. \b \s \n etc.
function keepCharsAbove(inStr, charCode) {
var goodChars = [];
for(var x = 0; x < inStr.length; x++) {
if(inStr.charCodeAt(x) > charCode) {
goodChars.push(inStr.charAt(x));
}
}
return goodChars.join("");
}
Usage:
keepCharsAbove("foo \t bar",32); // returns 'foobar'
本文标签: regexSimple ascii replacementJavascriptStack Overflow
版权声明:本文标题:regex - Simple ascii replacement - Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741504598a2382244.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论