user2034164
user2034164

Reputation: 303

Remove leading dots from start of text

I have some text that I need to target that has some leading dots, I have found some code that works with words and spacing between, but unable to get this to work for the leading dots. I have played with the regex, but it brakes things in a big way.

Here is what I have:

$(document).ready(function(){
 $('p').each(function () {
     var $this = $(this);
     $this.html($this.text().replace(/\b..\b/g, '<span style="display:none">..</span>'));
   });
});

http://jsfiddle.net/Xg3Ej/

Can anyone put me on the right path?

Upvotes: 2

Views: 2115

Answers (6)

doctorlove
doctorlove

Reputation: 19252

If you just want to remove characters from the start use ^, also escape the dot \., otherwise . matches any character. Finally, use + to indicate match one or more dots.

/^\.+/g

Upvotes: 1

Abhidev
Abhidev

Reputation: 7253

This will work too http://jsfiddle.net/Xg3Ej/11/

$this.html($this.text().replace('..', '<span style="display:none">..</span>'));

Upvotes: 0

user3064914
user3064914

Reputation: 959

Try this

This will capture the string starts with (..)

. => mean a single character in regex. Hence it is escaped using backslash.

example:

..jfghhfg

$this.html($this.text().replace(/^\.\./g, '<span style="display:none">..</span>')); 

Here is the demo

Upvotes: 0

techfoobar
techfoobar

Reputation: 66663

Use ^ in your regex to look at the start of the string.

/^\.\./g

Fiddle: http://jsfiddle.net/Xg3Ej/8/

Note: Thanks to Andrew Mackrodt for pointing out the escaping..

Upvotes: 3

Stephan
Stephan

Reputation: 43023

Why not trying this simply:

$(document).ready(function(){
$('p').each(function () {
    var $this = $(this);
   $this.html($this.text().replace(/^\.{2}/g, ''));
    });
});

Fiddle: http://jsfiddle.net/Xg3Ej/9/

Upvotes: 0

Andrew Mackrodt
Andrew Mackrodt

Reputation: 1826

. has a special meaning in regex, it will match any character. You need to escape it so that the regex matches a literal character \..

$this.text().replace(/^(\.+)/g, '<span style="display:none">$1</span>')

http://jsfiddle.net/Xg3Ej/6/

Upvotes: 7

Related Questions