如何编写正则表达式去除字符串中的段首段尾空格和全角空格?

2026-04-29 21:407阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

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

如何编写正则表达式去除字符串中的段首段尾空格和全角空格?

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

alert(去除字符串首尾空白的测试);

<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;和全角的空格" ")给予保留!

如何编写正则表达式去除字符串中的段首段尾空格和全角空格?

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

如何编写正则表达式去除字符串中的段首段尾空格和全角空格?

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

alert(去除字符串首尾空白的测试);

<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;和全角的空格" ")给予保留!

如何编写正则表达式去除字符串中的段首段尾空格和全角空格?