简体   繁体   English

JavaScript 正则表达式 - 从开头和结尾删除空格

[英]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:所以内部流程是:

  1. Look for all sections that match the regex (which will be the whitespace at the beginning and the whitespace at the end查找与正则表达式匹配的所有部分(将是开头的空格和结尾的空格
  2. Replace each match with "" , 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.

相关问题 使用正则表达式javascript从字符串中删除HTML标签和空格 - Remove HTML Tags and whitespace from string using regex javascript 正则表达式从字符串的开头和结尾删除某些字符 - Regex Expression to remove certain characters from string expect at the start and end Javascript正则表达式-删除字符,空格和开头0 - Javascript Regex - Remove Chars, WhiteSpace & Starting 0 javascript正则表达式删除空白失败,为什么? - javascript regex to remove whitespace fails, why? 什么正则表达式从Javascript字符串的开头和结尾开始修剪任意标点符号? - What regex trims arbitrary punctuation from the start and end of a string in Javascript? 在JavaScript中从字符串的开头到结尾删除HTML内容组 - Remove HTML content groups from start to end of string in JavaScript 使用jQuery或javascript从网页中存在的所有文本框的字符串开头和结尾删除空格 - Remove whitespace from beginning and end of string of all textbox present in a webpage using jQuery or javascript 正则表达式删除所有<br /> javascript中字符串开头和结尾的标签 - Regex to remove all <br /> tags from beginning and end of a string in javascript 正则表达式删除字符串中字符的开始到结尾 - Regex expression to remove start to end of character in string Javascript正则表达式可从字符串开头删除数字以外的任何内容 - Javascript regex to remove anything but numbers from the start of a string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM