From 6e1c82d78bd8f3e7778dc862ee72b092db5b5723 Mon Sep 17 00:00:00 2001 From: sakpal Date: Sun, 6 Sep 2020 14:59:16 +1000 Subject: [PATCH 1/2] add isAlphaNumeric snippet --- snippets/isAlphaNumeric.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 snippets/isAlphaNumeric.md diff --git a/snippets/isAlphaNumeric.md b/snippets/isAlphaNumeric.md new file mode 100644 index 000000000..9c07a4084 --- /dev/null +++ b/snippets/isAlphaNumeric.md @@ -0,0 +1,19 @@ +--- +title: isAlphaNumeric +tags: string,regexp,intermediate +--- + +Checks if a string contains only alphanumeric characters. + +Use `String.prototype.match()` to check if input string matches against alphanumeric regex pattern. + +```js +const isAlphaNumeric = (str) => !!str.match(/^[a-z0-9]+$/gi); +``` + +```js +isAlphaNumeric('hello123'); // true +isAlphaNumeric('123'); // true +isAlphaNumeric('hello 123'); // false (space character is not alphanumeric) +isAlphaNumeric('#$hello'); // false +``` From 268e563e472f567b2999592ad02efca0f33a4c58 Mon Sep 17 00:00:00 2001 From: Isabelle Viktoria Maciohsek Date: Mon, 7 Sep 2020 12:38:12 +0300 Subject: [PATCH 2/2] Update isAlphaNumeric.md --- snippets/isAlphaNumeric.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/snippets/isAlphaNumeric.md b/snippets/isAlphaNumeric.md index 9c07a4084..f496dfc84 100644 --- a/snippets/isAlphaNumeric.md +++ b/snippets/isAlphaNumeric.md @@ -1,14 +1,14 @@ --- title: isAlphaNumeric -tags: string,regexp,intermediate +tags: string,regexp,beginner --- Checks if a string contains only alphanumeric characters. -Use `String.prototype.match()` to check if input string matches against alphanumeric regex pattern. +Use `RegExp.prototype.test()` to check if input string matches against alphanumeric regex pattern. ```js -const isAlphaNumeric = (str) => !!str.match(/^[a-z0-9]+$/gi); +const isAlphaNumeric = str => /^[a-z0-9]+$/gi.test(str); ``` ```js