Regex to replace 'NO-BREAK SPACE'
Regex to replace 'NO-BREAK SPACE'
我正在寻找一个正则表达式来取代'NO-BREAK SPACE's从字符串。
在SO上有一些与'NO-BREAK SPACE'有关的问题,但似乎没有一个能给我指明正确的答案。
到目前为止,我尝试使用(字符串"A B"的第二个字符是一个没有中断的空格)没有成功:
"A B".replace(new RegExp(String.fromCharCode(160),"g"),"xxx");
"A B".replace($('<b> </b>').text(), 'xxx');
"A B".replace(/'xA0/,'xxx');
"A B".replace(/''xA0/,'xxx');
"A B".replace(/'u00A0/,'xxx');
"A B".replace(/''u00A0/,'xxx');
更新:愚蠢的我。事实是我用错误的字符测试了很长一段时间。
字符串中的实际字符是NARROW NO-BREAK SPACE (U+202F),而不是通常的'xA0
。
.replace(/['u202F'u00A0]/, "...")
除了空格,NO-BREAK space等,还有其他空格字符也可以出现在字符串中。
这里是空格的完整Unicode列表。来源:http://jkorpela.fi/chars/spaces.html
<表类>
数量角色名称
tbody> <<tr> ' u0020 td> ' u00A0 td> 中断空间/td> ' u1680 td> 姆空格 ' u180E td> 古语元音分隔符 ' u2000 td> 四道明> tr> ' u2001 td> em四道明> tr> ' u2002 td> n space(螺母) ' u2003 td> m space (mutton) ' u2004 td> 行三间距(粗间距) ' u2005 td> 行4个空格(中间空格) ' u2006 td> six-per-em空间/td> ' u2007 td> ' u2008 td> 点符号空间/td> ' u2009 td> 空间/td> ' u200A td> 发空间/td> ' u200B td> 宽度空间 ' u202F td> 不间断空格 ' u205F td> 等数学空间 ' u3000 td> 意空间td> tr> ' uFEFF td> 宽度空格 tbody> 表类>
我不知道@georg在他的回答中解释的事实。
但是如果你只是复制A
和B
之间的字符并将其粘贴到.replace()
函数中,它就可以工作。
小提琴来了。
和代码片段。
alert("A B".replace(' ', 'xxx'));
显然没有unicode分类涵盖这个用例。
@Rakesh回答中的regex在unicode-space列表中缺少一些字符,我需要c#风格的。
这里列表被转换成c#表达式,生成regex-pattern:
string.Concat("{", string.Join("|", new[]
{
new { c = ''u0020', desc = "space" },
new { c = ''u00A0', desc = "no-break space" },
new { c = ''u1680', desc = "Ogham space mark" },
new { c = ''u180E', desc = "Mongolian vowel separator" },
new { c = ''u2000', desc = "en quad" },
new { c = ''u2001', desc = "em quad" },
new { c = ''u2002', desc = "en space (nut)" },
new { c = ''u2003', desc = "em space (mutton)" },
new { c = ''u2004', desc = "three-per-em space (thick space)" },
new { c = ''u2005', desc = "four-per-em space (mid space)" },
new { c = ''u2006', desc = "six-per-em space" },
new { c = ''u2007', desc = "figure space" },
new { c = ''u2008', desc = "punctuation space" },
new { c = ''u2009', desc = "thin space" },
new { c = ''u200A', desc = "hair space" },
new { c = ''u200B', desc = "zero width space" },
new { c = ''u202F', desc = "narrow no-break space" },
new { c = ''u205F', desc = "medium mathematical space" },
new { c = ''u3000', desc = "ideographic space" },
new { c = ''uFEFF', desc = "zero width no-break space" },
}
.Select(a => $"''u{(int)a.c:X4}")
), "}")
// Become "{'u0020|'u00A0|'u1680|'u180E|'u2000|'u2001|'u2002|'u2003|'u2004|'u2005|'u2006|'u2007|'u2008|'u2009|'u200A|'u200B|'u202F|'u205F|'u3000|'uFEFF}"
用于在LINQPad中复制粘贴和查看:.Select(a => new { a.c, num = (int)a.c, part = $"''u{(int)a.c:X4}", a.desc })
相关文章:
- 使用while循环+break是一种可接受的方式;goto;手术的结束
- Javascript GetElementByID has no value
- 谷歌地图API v3:Initial View is Fine,but Gray Box with No Map if
- Javascript window.open()toolbar=no不起作用
- Regex表示10位数字,其中没有't允许所有零、所有单个no和序列号
- JavaScript:模拟'break'内部'foreach'环
- `npm install` killed for large package.json, no npm-debug.lo
- YES或NO表示对象事件中的分号
- Wordpress AJAX with raw javascript? NO Jquery
- How to replace space with " "
- 禁用复选框上的输入框 jquery NO onchange
- no-op if 语句会导致 Google 图表失败
- No puedo comparar una variable con un cadena de tipo String
- 在javascript中访问当前页面的页面标题[no ajax]
- JavaScript 动态更改图像 src 的位置 - NO JQuery
- RequireJs in IE8: "no define call for.."
- HTML DOMs, ids vs no ids?
- AngularJS - 在第一个可见元素上应用 CSS 类(Preferred No JQuery)
- Mustache js line break
- Regex to replace 'NO-BREAK SPACE'