From 2d919d88c6f15f4709bb95f5cb89587ab60e49fb Mon Sep 17 00:00:00 2001 From: Rahul Dahal <44515005+rahuldahal@users.noreply.github.com> Date: Tue, 13 Oct 2020 11:59:46 +0545 Subject: [PATCH 1/2] Create removeAllWhitespaces.md Uses `String.prototype.replace()` with a regular expression to replace any and all occurrences of whitespace characters with a empty string. --- snippets/removeAllWhitespaces.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 snippets/removeAllWhitespaces.md diff --git a/snippets/removeAllWhitespaces.md b/snippets/removeAllWhitespaces.md new file mode 100644 index 000000000..4eb0cf7f6 --- /dev/null +++ b/snippets/removeAllWhitespaces.md @@ -0,0 +1,22 @@ +--- +title: removeAllWhitespaces +tags: string,regexp,beginner +--- + + +Returns a string removing any and all whitespaces. +- Use `String.prototype.replace()` with a regular expression to replace any and all occurrences of whitespace characters with a empty string. + +```javascript +const removeAllWhitespaces = (string) => { + if(!string || typeof string !== "string") return ""; + return string.replace(/\s+/g, ""); // trimming the whitespace, if any +} +``` + + +```javascript + +removeAllWhitespaces(" Hello, I've a lot of white spaces. \n Including a line break."); // "Hello,I'vealotofwhitespaces.Includingalinebreak." + +``` From 800dd2b4ae047dc142d799ba23a2cedfd239d470 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Tue, 13 Oct 2020 09:37:17 +0300 Subject: [PATCH 2/2] Update and rename removeAllWhitespaces.md to removeWhitespace.md --- snippets/removeAllWhitespaces.md | 22 ---------------------- snippets/removeWhitespace.md | 16 ++++++++++++++++ 2 files changed, 16 insertions(+), 22 deletions(-) delete mode 100644 snippets/removeAllWhitespaces.md create mode 100644 snippets/removeWhitespace.md diff --git a/snippets/removeAllWhitespaces.md b/snippets/removeAllWhitespaces.md deleted file mode 100644 index 4eb0cf7f6..000000000 --- a/snippets/removeAllWhitespaces.md +++ /dev/null @@ -1,22 +0,0 @@ ---- -title: removeAllWhitespaces -tags: string,regexp,beginner ---- - - -Returns a string removing any and all whitespaces. -- Use `String.prototype.replace()` with a regular expression to replace any and all occurrences of whitespace characters with a empty string. - -```javascript -const removeAllWhitespaces = (string) => { - if(!string || typeof string !== "string") return ""; - return string.replace(/\s+/g, ""); // trimming the whitespace, if any -} -``` - - -```javascript - -removeAllWhitespaces(" Hello, I've a lot of white spaces. \n Including a line break."); // "Hello,I'vealotofwhitespaces.Includingalinebreak." - -``` diff --git a/snippets/removeWhitespace.md b/snippets/removeWhitespace.md new file mode 100644 index 000000000..1af98f2be --- /dev/null +++ b/snippets/removeWhitespace.md @@ -0,0 +1,16 @@ +--- +title: removeWhitespace +tags: string,regexp,beginner +--- + +Returns a string with whitespaces removed. + +- Use `String.prototype.replace()` with a regular expression to replace all occurrences of whitespace characters with an empty string. + +```js +const removeWhitespace = str => str.replace(/\s+/g,''); +``` + +```js +removeWhitespace('Lorem ipsum.\n Dolor sit amet. '); // 'Loremipsum.Dolorsitamet.' +```