23

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,522
  • 9
  • 31
  • 66

7 Answers7

38

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,589
  • 4
  • 34
  • 50
  • 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
  • 384,651
  • 66
  • 527
  • 531
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
  • 28,160
  • 11
  • 74
  • 110
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,743
  • 21
  • 27
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
  • 16,507
  • 9
  • 88
  • 138
1

Use RegEx with javaScript Replace

var res = s.replace(/(^\.+|\.+$)/mg, '');
Dhaval Marthak
  • 17,246
  • 6
  • 46
  • 68
0

We can use replace() method to remove the unwanted string in a string

Example:

var str = '<pre>I'm big fan of Stackoverflow</pre>'

str.replace(/<pre>/g, '').replace(/<\/pre>/g, '')

console.log(str)

output:

Check rules on RULES blotter

KARTHIKEYAN.A
  • 18,210
  • 6
  • 124
  • 133