Javascript删除开头和结尾的字符串

Javascript删除开头和结尾的字符串,javascript,Javascript,基于以下字符串 ...here.. ..there... .their.here. 如何使用javascript删除字符串开头和结尾的,就像删除所有空格的修剪一样 输出应该是 here there their.here 尝试使用以下正则表达式 var text = '...here..\n..there...\n.their.here.'; var replaced = text.replace(/(^\.+|\.+$)/mg, ''); 将正则表达式与javaScript一起使用 使

基于以下字符串

...here..
..there...
.their.here.
如何使用javascript删除字符串开头和结尾的
,就像删除所有空格的修剪一样

输出应该是

here
there
their.here

尝试使用以下正则表达式

var text = '...here..\n..there...\n.their.here.';
var replaced =  text.replace(/(^\.+|\.+$)/mg, '');

将正则表达式与javaScript一起使用

使用正则表达式
/(^\.+\.+$)/mg

  • ^
    开始时表示
  • \.+
    一个或多个句号
  • $
    表示末尾
因此:


下面是一个使用String.prototype的非正则表达式答案

String.prototype.strim = function(needle){
    var first_pos = 0;
    var last_pos = this.length-1;
    //find first non needle char position
    for(var i = 0; i<this.length;i++){
        if(this.charAt(i) !== needle){
            first_pos = (i == 0? 0:i);
            break;
        }
    }
    //find last non needle char position
    for(var i = this.length-1; i>0;i--){
        if(this.charAt(i) !== needle){
            last_pos = (i == this.length? this.length:i+1);
            break;
        }
    }
    return this.substring(first_pos,last_pos);
}
alert("...here..".strim('.'));
alert("..there...".strim('.'))
alert(".their.here.".strim('.'))
alert("hereagain..".strim('.'))
String.prototype.strim=函数(指针){
var first_pos=0;
var last_pos=此长度-1;
//找到第一个非针字符位置
对于(var i=0;i0;i--){
if(this.charAt(i)!==指针){
last_pos=(i==this.length?this.length:i+1);
打破
}
}
返回此子字符串(第一个位置,最后一个位置);
}
警惕(“…这里…”strim('.');
警报(“…那里…”strim('.'))
警惕(“.this.here.”.strim(“.”)
警报(“再次在此…”strim('.'))

在这里查看它的工作情况:

这就是为什么此任务的正则表达式是
/(^\+\\.+$)/mg

  • /()/
    内部,您可以编写要在字符串中找到的子字符串的模式:

    /(ol)/
    这将在字符串中找到子字符串
    ol

    替换(/(ol)/,“a”)将为您提供
    x==“cat”

  • /()/
    中的
    ^\.+\.+$
    通过符号
    [表示或]

    ^\.+
    \.+$

  • ^\.+
    意味着在开始时找到尽可能多的

    ^
    表示开始时;\是逃避性格;在字符后面添加
    +
    意味着匹配包含一个或多个该字符的任何字符串

  • \.+$
    意味着在末尾找到尽可能多的

    $
    表示结尾处

  • /()/
    后面的
    m
    用于指定如果字符串具有换行符或回车符,^和$运算符现在将匹配换行符边界,而不是字符串边界

  • /()/
    后面的
    g
    用于执行全局匹配:因此它查找所有匹配项,而不是在第一次匹配后停止


  • 要了解有关RegEx的更多信息,您可以查看。

    稍微多一些代码golfy(如果不可读),非RegEx原型扩展:

    String.prototype.strim = function(needle)   {
        var out = this;
        while (0 === out.indexOf(needle))
            out = out.substr(needle.length);
        while (out.length === out.lastIndexOf(needle) + needle.length)
            out = out.slice(0,out.length-needle.length);
        return out;
    }
    
    var spam = "this is a string that ends with thisthis";
    alert("#" + spam.strim("this") + "#");
    

    解释得很好。谢谢@吖奇说-何魏奇ArchyWillHe,如果有不同的字符串,如
    。--“some-name.”--
    我只想保留
    某个名称,应该做什么更改。我有一个例子,字符串可以有
    -'。
    在开始或结束的次数,我想删除它们
    String.prototype.strim = function(needle){
        var first_pos = 0;
        var last_pos = this.length-1;
        //find first non needle char position
        for(var i = 0; i<this.length;i++){
            if(this.charAt(i) !== needle){
                first_pos = (i == 0? 0:i);
                break;
            }
        }
        //find last non needle char position
        for(var i = this.length-1; i>0;i--){
            if(this.charAt(i) !== needle){
                last_pos = (i == this.length? this.length:i+1);
                break;
            }
        }
        return this.substring(first_pos,last_pos);
    }
    alert("...here..".strim('.'));
    alert("..there...".strim('.'))
    alert(".their.here.".strim('.'))
    alert("hereagain..".strim('.'))
    
    String.prototype.strim = function(needle)   {
        var out = this;
        while (0 === out.indexOf(needle))
            out = out.substr(needle.length);
        while (out.length === out.lastIndexOf(needle) + needle.length)
            out = out.slice(0,out.length-needle.length);
        return out;
    }
    
    var spam = "this is a string that ends with thisthis";
    alert("#" + spam.strim("this") + "#");