Regular Expressions

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Define Negated character sets.

Characters that we don't want to match.To create a negated character set, you place a caret character (^) after the opening bracket and before the characters you do not want to match. For example, /[^aeiou]/gi matches all characters that are not a vowel. Note that characters like ., !, [, @, / and white space are matched - the negated vowel character set only excludes the vowel characters.

Define global flag?

For repeating let twinkleStar = "Twinkle, twinkle, little star"; let starRegex = /twinkle/gi; // Change this line let result = twinkleStar.match(starRegex); // returns twinke, twinkle

Define character set and hyphen.

Inside a character set, you can define a range of characters to match using a hyphen character: -. let catStr = "cat"; let batStr = "bat"; let matStr = "mat"; let bgRegex = /[a-e]at/; Using the hyphen (-) to match a range of characters is not limited to letters. It also works to match a range of numbers.

Define test( )?

JavaScript has multiple ways to use regexes. One way to test a regex is using the .test() method. The .test() method takes the regex, applies it to a string (which is placed inside the parentheses), and returns true or false if your pattern finds something or not. let testStr = "freeCodeCamp"; let testRegex = /Code/; testRegex.test(testStr); //returns true

The caret ^ outside of a character set.

Outside of a character set, the caret is used to search for patterns at the beginning of strings. let firstString = "Ricky is first and can be found."; let firstRegex = /^Ricky/; firstRegex.test(firstString); // Returns true

What are regex?

Regular expressions are special strings that represent a search pattern. Also known as "regex" or "regexp", they help programmers match, search, and replace text.

What's difference between match and test?

Reverse placing.

Define match( )?

So far, you have only been checking if a pattern exists or not within a string. You can also extract the actual matches you found with the .match() method. let ourStr = "Regular expressions"; let ourRegex = /expressions/; ourStr.match(ourRegex); // Returns ["expressions"]

Flag i?

The flag that ignores case - the i flag.

The * sign.

The plus + sign is used to look for characters that occur one or more times. There's also an option that matches characters that occur zero or more times. The character to do this is the asterisk or star: *. let soccerWord = "gooooooooal!"; let gPhrase = "gut feeling"; let oPhrase = "over the moon"; let goRegex = /go*/; soccerWord.match(goRegex); // Returns ["goooooooo"] gPhrase.match(goRegex); // Returns ["g"] oPhrase.match(goRegex); // Returns null let chewieQuote = "Aaaaaaaaaaaaaaaarrrgh!"; let chewieRegex = /Aa*/; let result = chewieQuote.match(chewieRegex);

Match All Numbers

The shortcut to look for digit characters is \d, with a lowercase d. This is equal to the character class [0-9], which looks for a single character of any number between zero and nine. let numString = "Your sandwich will be $5.00"; let numRegex = /\d/g; // Change this line let result = numString.match(numRegex);//returns [ '5', '0', '0' ]

Match All Non-Numbers

The shortcut to look for non-digit characters is \D. This is equal to the character class [^0-9], which looks for a single character that is not a number between zero and nine.

Define the wildcard character.

The wildcard character . will match any one character. The wildcard is also called dot and period. You can use the wildcard character just like any other character in the regex. For example, if you wanted to match "hug", "huh", "hut", and "hum", you can use the regex /hu./ to match all four words.

Alternation operator?

This is powerful to search single strings, but it's limited to only one pattern. You can search for multiple patterns using the alternation or OR operator: |. /yes|no|maybe/

Define character classes.

You can search for a literal pattern with some flexibility with character classes. Character classes allow you to define a group of characters you wish to match by placing them inside square ([ and ]) brackets. let quoteSample = "Beware of bugs in the above code; I have only proved it correct, not tried it."; let vowelRegex = /[aeiou]/ig; let result = quoteSample.match(vowelRegex);

Match Everything But Letters and Numbers

You can search for the opposite of the \w with \W. Note, the opposite pattern uses a capital letter. This shortcut is the same as [^A-Za-z0-9_]. let shortHand = /\W/; let numbers = "42%"; let sentence = "Coding!"; numbers.match(shortHand); // Returns ["%"] sentence.match(shortHand); // Returns ["!"]

The dollar sign character $.

You can search the end of strings using the dollar sign character $ at the end of the regex. let theEnding = "This is a never ending story"; let storyRegex = /story$/; storyRegex.test(theEnding); // Returns true

Define quantity specifiers.

You can specify the lower and upper number of patterns with quantity specifiers using curly brackets. Sometimes you only want to specify the lower number of patterns with no upper limit. To only specify the lower number of patterns, keep the first number followed by a comma. For example, to match only the string "hah" with the letter a appearing at least 3 times, your regex would be /ha{3,}h/. let A4 = "haaaah"; let A2 = "haah"; let A100 = "h" + "a".repeat(100) + "h"; let multipleA = /ha{3,}h/; multipleA.test(A4); // Returns true multipleA.test(A2); // Returns false multipleA.test(A100); // Returns true

Define + character.

You can use the + character to check if that is the case. Remember, the character or pattern has to be present consecutively. That is, the character has to repeat one after the other. For example, /a+/g would find one match in "abc" and return ["a"]. Because of the +, it would also find a single match in "aabc" and return ["aa"]. If it were instead checking the string "abab", it would find two matches and return ["a", "a"] because the a characters are not in a row - there is a b between them. Finally, since there is no "a" in the string "bcd", it wouldn't find a match.

Match All Letters and Numbers

he closest character class in JavaScript to match the alphabet is \w. This shortcut is equal to [A-Za-z0-9_]. This character class matches upper and lowercase letters plus numbers. Note, this character class also includes the underscore character ( _ ). let longHand = /[A-Za-z0-9_]+/; let shortHand = /\w+/; let numbers = "42"; let varNames = "important_var"; longHand.test(numbers); // Returns true

Specify Exact Number of Matches with quantity specifiers.

let A4 = "haaaah"; let A3 = "haaah"; let A100 = "h" + "a".repeat(100) + "h"; let multipleHA = /a{3}h/; multipleHA.test(A4); // Returns false multipleHA.test(A3); // Returns true multipleHA.test(A100); // Returns false

Sometimes the patterns you want to search for may have parts of it that may or may not exist. However, it may be important to check for them nonetheless. You can specify the possible existence of an element with a question mark, ?. This checks for zero or one of the preceding element. You can think of this symbol as saying the previous element is optional.

let american = "color"; let british = "colour"; let rainbowRegex= /colou?r/; rainbowRegex.test(american); // Returns true rainbowRegex.test(british); // Returns true

let teststr = /[a-z]/g; let str = 'cn'; str.match(teststr);//[ 'c', 'n' ]

let teststr = /[a-z]+/; let str = 'cn'; str.match(teststr);//'cn'

Regular Expressions: Restrict Possible Usernames Usernames are used everywhere on the internet. They are what give users a unique identity on their favorite sites. You need to check all the usernames in a database. Here are some simple rules that users have to follow when creating their username. 1) The only numbers in the username have to be at the end. There can be zero or more of them at the end. 2) Username letters can be lowercase and uppercase. 3) Usernames have to be at least two characters long. A two-letter username can only use alphabet letter characters.

let username = "JackOfAllTrades"; let userCheck = /[a-zA-Z][a-zA-Z]+[0-9]*/i; // Change this line let result = userCheck.test(username);


Ensembles d'études connexes

Old Testament Survey, Final Exam

View Set

Spending Chain Manage Purchase Requisitions Course

View Set

Ch.3 Working with Financial Statements

View Set

Multiplying and Dividing Fractions

View Set

History 1112: Midterm Study Guide- Chapters 15 to 21 (Lessons 1-7)

View Set

final exam advertising study guide

View Set

Unit 8 Metabolism, nutrition, energetics

View Set