gpt4 book ai didi

regex - 在 Javascript 中匹配 IE7 和 IE8 中的不可破坏空格

转载 作者:行者123 更新时间:2023-12-04 21:20:21 26 4
gpt4 key购买 nike

我想在任何相对现代的浏览器中替换空格(因此对于 IE,版本 >= 7)。

因此给定字符串 "Hello world!"我们会这样做:

<script type="text/javascript">
document.write("Result: '" + "Hello world!".replace(/\s/g, '') + "'");
</script>

我们希望输出: Result: 'Helloworld!'
但是在 IE7 和 IE8 中,虽然它使用不可破坏的空间如以下之一失败: &#160; == &nbsp; == \u00A0
例如:
<script type="text/javascript">
document.write("Result: '" + String.fromCharCode(160).replace(/\s/g, '') + "'");
</script>

将输出 Result: 'Helloworld!'在 FF 和 IE >= 9 和 Result : ' '在 IE7 和 IE8 中。我勒个去?

这让我想知道这是否是唯一的异常(exception)?我根本找不到关于此的太多信息。是否有正则表达式可以删除所有空格,包括不可破坏的空格?

最佳答案

使用这个:

replace(/(?:\s|\xA0|&nbsp;|&#160;)+/g, '')
replace(/[\s\xA0]+/g, '')

关于regex - 在 Javascript 中匹配 IE7 和 IE8 中的不可破坏空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13211077/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com