21

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

the output should be

here
there
their.here
Snippet
  • 1,462
  • 9
  • 31
  • 66

6 Answers6

36

These are the reasons why the RegEx for this task is /(^\.+|\.+$)/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 ol in 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 m behind /()/ 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 g behind /()/ is used to perform a global match: so it find all matches rather than stopping after the first match.

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

Archy Will He 何魏奇
  • 9,367
  • 4
  • 30
  • 49
  • Well explained. Thanks. – Tushar Shukla Apr 15 '20 at 09:06
  • 1
    @吖奇说-何魏奇ArchyWillHe, what if there are different string like `..--'some-name.'--` I just want to keep `some-name`, what change should be done. I have a case where string can have `-'.` at start or end number of times and i want to remove them – Rohit Ambre Aug 13 '20 at 14:52
12

Try to use the following regex

var text = '...here..\n..there...\n.their.here.';
var replaced =  text.replace(/(^\.+|\.+$)/mg, '');
Arun P Johny
  • 376,738
  • 64
  • 519
  • 520
3

Here is working Demo

Use Regex /(^\.+|\.+$)/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, ''));
Zaheer Ahmed
  • 27,470
  • 11
  • 72
  • 109
3

Here is an non regular expression answer which utilizes 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/

Kemal Dağ
  • 2,715
  • 20
  • 26
2

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

ruffin
  • 15,005
  • 8
  • 80
  • 126
1

Use RegEx with javaScript Replace

var res = s.replace(/(^\.+|\.+$)/mg, '');
Dhaval Marthak
  • 16,966
  • 6
  • 41
  • 68