str=str.replace(/^\s+|\s+$/g,""); //去左右空格
str=str.replace(/^\s*/, ""); //去左空格
str=str.replace(/\s*$/, ""); //去右空格
str=str.replace(/\s+/g, ""); //去所有空格
用javascript去掉字符串段首和段尾的全角和半角空格.
1、去段首段尾的空格(包括半角的空格和全角的空格" ")
2、段中的空格(包括半角的空格和全角的空格" ")给予保留!
<script type="text/javascript">
String.prototype.trim = function(){
return this.replace(/^( |[\s ])+|( |[\s ])+$/g, "");
}
alert("---"+ " this is a test spacing ".trim() + "---");
</script>
若想去掉字符串中的所有空格包括段中的,将以上正则改为:
this.replace(/( |[\s ])/g, "");
即可.
========================
1.String.prototype.Trim = function()
{
return this.replace(/(^\s*)|(\s*$)/g, "");
}
String.prototype.LTrim = function()
{
return this.replace(/(^\s*)/g, "");
}
String.prototype.Rtrim = function()
{
return this.replace(/(\s*$)/g, "");
}
2.全文匹配替换
var regExp = new RegExp("需要被替换的字符',"g")
var text = "…………";
text = text.replace(regExp , "替换的字符");
3.方案
String .prototype.trim = function(){
var matches = this.match(/^[ \t\n\r]+/);
var prefixLength = (matches == null) ? 0:matches[0].length;
matches = this.match(/[ \t\r\n]+$/);
var suffixLength = (matches == null) ? 0:matches[0].length;
return this.slice(prefixLength,this.length-suffixLength);
}
========================
如
s=" hello welcome to china "
转换成"hello welcome to china"
(原来是连续多个空格的变成一个空格,原来是一个空格的不变) :
s=trim(RegReplace(s,"\s+"," "))
=============================
//删除字符串俩端的空格
String.prototype.Trim = function()
{
return this.replace(/(^\s*)|(\s*$)/g, "");
}
//删除字符串左端的空格
String.prototype.LTrim = function()
{
return this.replace(/(^\s*)/g, "");
}
//删除字符串右端的空格
String.prototype.RTrim = function()
{
return this.replace(/(\s*$)/g, "");
}
//还是去掉空格
String.prototype.trim=function()
{
return this.replace(/(^[\s\t\xa0\u3000]+)|([\u3000\xa0\s\t]+$)/g, "");
};
//判断是否是图片的url地址
function isImgUrl(str_url){
var regex="^(http|ftp)://(www.)?.+.*.+[.{1}](jpeg|JPEG|GIF|gif|bmp|BMP|jpg|JPG|PNG|png){1}$";
var re=new RegExp(regex);
if (re.test(str_url)){
return (true);
}else{
return (false);
}
}
//是否是合法的url地址
function IsURL(str_url){
var strRegex = "^((https|http|ftp|rtsp|mms)?://)"
+ "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?" //ftp的user@
+ "(([0-9]{1,3}\.){3}[0-9]{1,3}" // IP形式的URL- 199.194.52.184
+ "|" // 允许IP和DOMAIN(域名)
+ "([0-9a-z_!~*'()-]+\.)*" // 域名- www.
+ "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\." // 二级域名
+ "[a-z]{2,6})" // first level domain- .com or .museum
+ "(:[0-9]{1,4})?" // 端口- :80
+ "((/?)|" // a slash isn't required if there is no file name
+ "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$";
var re=new RegExp(strRegex);
//re.test()
if (re.test(str_url)){
return (true);
}else{
return (false);
}
}
str=str.replace(/^\s*/, ""); //去左空格
str=str.replace(/\s*$/, ""); //去右空格
str=str.replace(/\s+/g, ""); //去所有空格
用javascript去掉字符串段首和段尾的全角和半角空格.
1、去段首段尾的空格(包括半角的空格和全角的空格" ")
2、段中的空格(包括半角的空格和全角的空格" ")给予保留!
<script type="text/javascript">
String.prototype.trim = function(){
return this.replace(/^( |[\s ])+|( |[\s ])+$/g, "");
}
alert("---"+ " this is a test spacing ".trim() + "---");
</script>
若想去掉字符串中的所有空格包括段中的,将以上正则改为:
this.replace(/( |[\s ])/g, "");
即可.
========================
1.String.prototype.Trim = function()
{
return this.replace(/(^\s*)|(\s*$)/g, "");
}
String.prototype.LTrim = function()
{
return this.replace(/(^\s*)/g, "");
}
String.prototype.Rtrim = function()
{
return this.replace(/(\s*$)/g, "");
}
2.全文匹配替换
var regExp = new RegExp("需要被替换的字符',"g")
var text = "…………";
text = text.replace(regExp , "替换的字符");
3.方案
String .prototype.trim = function(){
var matches = this.match(/^[ \t\n\r]+/);
var prefixLength = (matches == null) ? 0:matches[0].length;
matches = this.match(/[ \t\r\n]+$/);
var suffixLength = (matches == null) ? 0:matches[0].length;
return this.slice(prefixLength,this.length-suffixLength);
}
========================
如
s=" hello welcome to china "
转换成"hello welcome to china"
(原来是连续多个空格的变成一个空格,原来是一个空格的不变) :
s=trim(RegReplace(s,"\s+"," "))
=============================
//删除字符串俩端的空格
String.prototype.Trim = function()
{
return this.replace(/(^\s*)|(\s*$)/g, "");
}
//删除字符串左端的空格
String.prototype.LTrim = function()
{
return this.replace(/(^\s*)/g, "");
}
//删除字符串右端的空格
String.prototype.RTrim = function()
{
return this.replace(/(\s*$)/g, "");
}
//还是去掉空格
String.prototype.trim=function()
{
return this.replace(/(^[\s\t\xa0\u3000]+)|([\u3000\xa0\s\t]+$)/g, "");
};
//判断是否是图片的url地址
function isImgUrl(str_url){
var regex="^(http|ftp)://(www.)?.+.*.+[.{1}](jpeg|JPEG|GIF|gif|bmp|BMP|jpg|JPG|PNG|png){1}$";
var re=new RegExp(regex);
if (re.test(str_url)){
return (true);
}else{
return (false);
}
}
//是否是合法的url地址
function IsURL(str_url){
var strRegex = "^((https|http|ftp|rtsp|mms)?://)"
+ "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?" //ftp的user@
+ "(([0-9]{1,3}\.){3}[0-9]{1,3}" // IP形式的URL- 199.194.52.184
+ "|" // 允许IP和DOMAIN(域名)
+ "([0-9a-z_!~*'()-]+\.)*" // 域名- www.
+ "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\." // 二级域名
+ "[a-z]{2,6})" // first level domain- .com or .museum
+ "(:[0-9]{1,4})?" // 端口- :80
+ "((/?)|" // a slash isn't required if there is no file name
+ "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$";
var re=new RegExp(strRegex);
//re.test()
if (re.test(str_url)){
return (true);
}else{
return (false);
}
}