[英]JavaScript Regex - Remove Whitespace from Start and End
I worked on the below challenge for about 3 hours and none of my code was working.我在下面的挑战中工作了大约 3 个小时,但我的代码都没有工作。 Decided to look at the solution to understand why I was not working.决定查看解决方案以了解为什么我不工作。 When I looked at the solution I was confused because I thought that \\s to identify white spaces not to remove them... can someone give me hand and explain why the usage of \\s instead of \\S and why using the empty string ("") to get rid of the white spaces on both ends.当我查看解决方案时,我很困惑,因为我认为 \\s 来识别空格而不是删除它们......有人可以帮我解释为什么使用 \\s 而不是 \\S 以及为什么使用空字符串 ( "") 去掉两端的空格。
CHALLENGE挑战
Write a regex and use the appropriate string methods to remove whitespace at the beginning and end of strings.编写一个正则表达式并使用适当的字符串方法去除字符串开头和结尾的空格。
//SOLUTION
let hello = " Hello, World! ";
let wsRegex = /^\s+|\s+$/g;
let result = hello.replace(wsRegex, "");
The second argument of replace
is for what you will replace from the match(es) of the first argument. replace
的第二个参数是您将从第一个参数的匹配项中替换的内容。
The regex will match/select the spaces on the beginning (^) and on the end ($) of the string, and then will be replaced by "".正则表达式将匹配/选择字符串开头 (^) 和结尾 ($) 的空格,然后将替换为 ""。
When you use the regex /(\\S)/g
you're matching everything but spaces, in this case you will use something like hello.replace(/(\\S)/g, '$1')
;当您使用正则表达式/(\\S)/g
您将匹配除空格之外的所有内容,在这种情况下,您将使用hello.replace(/(\\S)/g, '$1')
;
$1
means the first group of your regex. $1
表示正则表达式的第一组。
\\s
means whitespace characters in regex, like <space>, <tab>, etc. \\s
表示正则表达式中的空白字符,例如 <space>、<tab> 等。^
means the beginning of the string ^
表示字符串的开头$
means the end of the string $
表示字符串的结尾|
means OR (match the left side or the right side)表示 OR(匹配左侧或右侧)+
means 1 or more (based off of the rule on the left) +
表示 1 个或更多(基于左侧的规则)/a regex/g
the g
means "global", aka "match multiple times" since you could need to match at the beginning AND end /a regex/g
g
表示“全局”,也就是“多次匹配”,因为您可能需要在开头和结尾进行匹配So the regex means:所以正则表达式的意思是:
/^\s+|\s+$/g
/ / Wrap the regex (how you do it in JS)
^\s+ Try to match at the beginning one or more whitespace chars
| Or...
\s+$ Try to match whitespace chars at the end
g Match as many times as you can
String.prototype.replace replaces the match(es) found in the regex with the string provided as the 2nd argument, in this case an empty string. String.prototype.replace用作为第二个参数提供的字符串替换在正则表达式中找到的匹配项,在本例中为空字符串。
So the process internally is:所以内部流程是:
""
, removing those matches entirely用""
替换每个匹配项,完全删除这些匹配项 let hello = " Hello, World! "; let wsRegex = /^\\s+|\\s+$/g; let result = hello.replace(wsRegex, ""); console.log('"' + result + '"');
Most people use String.prototype.replaceAll instead of .replace
when they use the global flag (大多数人在使用全局标志时使用String.prototype.replaceAll而不是.replace
(
let hello = " Hello, World! "; let wsRegex = /^\\s+|\\s+$/g; let result = hello.replaceAll(wsRegex, ""); console.log('"' + result + '"');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.