[英]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/ http://jsfiddle.net/Xg3Ej/
Can anyone put me on the right path? 任何人都可以把我放在正确的道路上吗?
.
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>')
Use ^
in your regex to look at the start of the string. 在正则表达式中使用^
来查看字符串的开头。
/^\.\./g
Fiddle: http://jsfiddle.net/Xg3Ej/8/ 小提琴: http : //jsfiddle.net/Xg3Ej/8/
Note: Thanks to Andrew Mackrodt for pointing out the escaping.. 注意:感谢Andrew Mackrodt指出逃脱..
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
Try this 尝试这个
This will capture the string starts with (..) 这将捕获以(..)开头的字符串
. 。 => mean a single character in regex. =>表示正则表达式中的单个字符。 Hence it is escaped using backslash. 因此它使用反斜杠进行转义。
example: 例:
..jfghhfg ..jfghhfg
$this.html($this.text().replace(/^\.\./g, '<span style="display:none">..</span>'));
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/ 小提琴: http : //jsfiddle.net/Xg3Ej/9/
这也会起作用http://jsfiddle.net/Xg3Ej/11/
$this.html($this.text().replace('..', '<span style="display:none">..</span>'));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.