基于以下字符串
...here..
..there...
.their.here.
我如何使用 javascript 删除字符串开头和结尾的 .
,就像删除所有空格的 trim
输出应该是
here
there
their.here
最佳答案
这些是此任务的 RegEx 为 /(^\.+|\.+$)/mg
的原因:
在
/()/
中,您可以在字符串中写入要查找的子字符串的模式:/(ol)/
This will find the substringol
in the string.var x = "colt".replace(/(ol)/, 'a');
会给你x == "cat"
;/()/
中的^\.+|\.+$
被|
分隔成两部分> [意味着或]^\.+
and\.+$
^\.+
表示在开头尽可能多的找到.
。^
means at the start; \ is to escape the character; adding+
behind a character means to match any string containing one or more that character\.+$
意思是在最后尽可能多的找到.
$
means at the end.
/()/
后面的m
用于指定如果字符串有换行符或回车符,则 ^ 和 $ 运算符现在将匹配针对换行符边界,而不是字符串边界。/()/
后面的g
用于执行全局匹配:因此它会找到所有匹配而不是在第一个匹配后停止。
要了解有关 RegEx 的更多信息,您可以查看 this guide .
关于Javascript 删除开头和结尾的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19134860/