我不知道这是如何工作的:
x=x.replace(/^\s+|\s+$/g,"");
什么是竖线(|)?
最佳答案
竖线表示“或”。
所以你的正则表达式匹配
^ # the start of the string
/s+ # followed by whitespace (one or more characters)
| # or
/s+ # whitespace
$ # followed by the end of the string
/g
(全局)修饰符将正则表达式应用于字符串中的所有匹配项,而不仅仅是第一个匹配项。
关于javascript - 这个 JavaScript RegExp 是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9878839/