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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19134860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 14:30:34  来源:igfitidea点击:

Javascript Remove strings in beginning and end

javascript

提问by Snippet

base on the following string

基于以下字符串

...here..
..there...
.their.here.

How can i remove the .on the beginning and end of string like the trim that removes all spaces, using javascript

如何.使用javascript删除字符串的开头和结尾,例如删除所有空格的修剪

the output should be

输出应该是

here
there
their.here

回答by u2041954

These are the reasons why the RegEx for this task is /(^\.+|\.+$)/mg:

这些是此任务的 RegEx 的原因/(^\.+|\.+$)/mg

  1. Inside /()/is where you write the pattern of the substring you want to find in the string:

    /(ol)/This will find the substring olin the string.

    var x = "colt".replace(/(ol)/, 'a');will give you x == "cat";

  2. The ^\.+|\.+$in /()/is separated into 2 parts by the symbol |[means or]

    ^\.+and \.+$

    1. ^\.+means to find as many .as possible at the start.

      ^means at the start; \ is to escape the character; adding +behind a character means to match any string containing one or more that character

    2. \.+$means to find as many .as possible at the end.

      $means at the end.

  3. The mbehind /()/is used to specify that if the string has newline or carriage return characters, the ^ and $ operators will now match against a newline boundary, instead of a string boundary.

  4. The gbehind /()/is used to perform a global match: so it find all matches rather than stopping after the first match.

  1. 里面/()/是你写你想在字符串中找到的子字符串的模式的地方:

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

    var x = "colt".replace(/(ol)/, 'a');会给你 x == "cat"

  2. 所述^\.+|\.+$/()/由符号分离成2份|[手段或]

    ^\.+\.+$

    1. ^\.+意味着.在开始时尽可能多地找到。

      ^意味着在开始;\ 是对字符进行转义;+在字符后面添加 意味着匹配包含一个或多个该字符的任何字符串

    2. \.+$意味着在最后找到尽可能多.的。

      $最后的意思。

  3. m后面/()/用于指定,如果字符串有换行或回车符,^和$工作人员将会进行匹配,而不是一个串边界对一个换行符边界。

  4. g后面/()/用于执行一个全球性的比赛:所以找到所有的比赛,而不是第一场比赛后停止。

To learn more about RegEx you can check out this guide.

要了解有关 RegEx 的更多信息,您可以查看本指南

回答by Arun P Johny

Try to use the following regex

尝试使用以下正则表达式

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

回答by Zaheer Ahmed

Here is working Demo

这是工作演示

Use Regex /(^\.+|\.+$)/mg

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

  • ^represent at start
  • \.+one or many full stops
  • $represents at end
  • ^开始时代表
  • \.+一个或多个句号
  • $代表结束

so:

所以:

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

回答by Kemal Da?

Here is an non regular expression answer which utilizes String.prototype

这是一个使用 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('.'))

and see it working here : http://jsfiddle.net/cettox/VQPbp/

并看到它在这里工作:http: //jsfiddle.net/cettox/VQPbp/

回答by Dhaval Marthak

Use RegEx with javaScript Replace

使用 RegEx 和 javaScript替换

var res = s.replace(/(^\.+|\.+$)/mg, '');

回答by ruffin

Slightly more code-golfy, if not readable, non-regexp prototype extension:

如果不可读,则代码更复杂,非正则表达式原型扩展:

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") + "#");

Fiddle-ige

小提琴