Reputation: 303
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>'));
});
});
Can anyone put me on the right path?
Upvotes: 2
Views: 2115
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
Reputation: 7253
This will work too http://jsfiddle.net/Xg3Ej/11/
$this.html($this.text().replace('..', '<span style="display:none">..</span>'));
Upvotes: 0
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
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
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
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>')
Upvotes: 7