如何用正则表达式去除文本首尾全角空格及连续空格?

2026-03-30 08:170阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计149个文字,预计阅读时间需要1分钟。

如何用正则表达式去除文本首尾全角空格及连续空格?

javascript// 去除字符串首尾空白字符String.prototype.trim=function() { return this.replace(/(\s|\n)+/g, '');};

alert(----\n\nthis is a test kwgkwg\n\n.trim()----);

<script language="javascript">
<!--
String.prototype.trim = function(){
return this.replace(/^(&nbsp;|[\s ])+|(&nbsp;|[\s ])+$/g, "" );
}
alert("---"+ " &nbsp; &nbsp;this is a test kwgkwg  &nbsp; ".trim() + "---");
/ --></script>
1、去段首段尾的空格(包括半角的空格代码&nbsp;和全角的空格" ")
2、段中的空格(包括半角的空格代码&nbsp;和全角的空格" ")给予保留!

如何用正则表达式去除文本首尾全角空格及连续空格?

本文共计149个文字,预计阅读时间需要1分钟。

如何用正则表达式去除文本首尾全角空格及连续空格?

javascript// 去除字符串首尾空白字符String.prototype.trim=function() { return this.replace(/(\s|\n)+/g, '');};

alert(----\n\nthis is a test kwgkwg\n\n.trim()----);

<script language="javascript">
<!--
String.prototype.trim = function(){
return this.replace(/^(&nbsp;|[\s ])+|(&nbsp;|[\s ])+$/g, "" );
}
alert("---"+ " &nbsp; &nbsp;this is a test kwgkwg  &nbsp; ".trim() + "---");
/ --></script>
1、去段首段尾的空格(包括半角的空格代码&nbsp;和全角的空格" ")
2、段中的空格(包括半角的空格代码&nbsp;和全角的空格" ")给予保留!

如何用正则表达式去除文本首尾全角空格及连续空格?