问题描述
基于以下字符串
...这里..
..来...
.their.here。
如何删除。
字符串的开头和结尾,如删除所有空格的修剪,使用javascript
输出应为
这里
有
他们。
以下是此任务的RegEx为 /(^ \。+ | \。+ $)/ mg
的原因:
-
在
/()/
内写下模式您要在字符串中找到的子字符串:var x =colt.replace(/(ol)/,'a');
会给你x ==cat
; -
^ \。+ | \。+ $
in/()/
由符号<$分为2部分c $ c> | [表示或]
-
^ \。+
表示在开始时尽可能多地找到。
。 -
\。+ $
表示尽可能多地找到。
。var x = "colt".replace(/(ol)/, 'a');
will give youx == "cat"
; The
^\.+|\.+$
in/()/
is separated into 2 parts by the symbol|
[means or]^\.+
means to find as many.
as possible at the start.\.+$
means to find as many.
as possible at the end.
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.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.
这篇关于Javascript在开头和结尾删除字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
-