Regex

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Operator to find match at the end of a string.

$

Capture group syntax when used in the replace function.

$1 $2 etc. If the string is "hot dog" and you pass /(\w) \s (\w)/ in as the regex then in replace you put $3 $2 it will return "dog hot"

Match zero or more of the preceding one character or set

*

Match zero or more of previous character or set

* as in /t*rust/

Match ONE or more of the previous character or set

+

Match ONE character or anything

. wildcard

Write a regex that matches Bob Smith or Sam Smith.

/(Bob | Sam) Smith/ note that it's not /(Bob) | (Sam) Smith/

Use a capture group to match "chicken chicken chicken dinner"

/(chicken) \1 \1 dinner/

Write a Regex that matches a string with any character but at least one time.. then rewrite it to be zero or more times

/.+/g (one or more) /.+*/g (for zero or more)

Matches one number from 1-5 or one letter from a-e irrespective of case

/[1-5a-e]/i

Match the character ZERO or ONE time.

? but only after following a character or character group.

Make it so that the regex will only find the item you specify if it's at the beginning of a word.

A carat ^ outside of a character set. (If it's inside a character set it will just negate it - which is a totally different function). /^test/ will only find test in this string "test is this" it will not find it in "this is test"

What will this capture group match? /([a-z])([A-Z])/g

Anything where there is a lowercase letter followed by an uppercase letter.

Negated character set, i.e. don't match any of these characters ONE time (unless followed by a quantifier like * or +)

add a ^, such as [^12345] means it won't match 1234 or 5

Will this match bbbbbbush? /[bm]+ush?

Yes. Write a Regex that returns bbbbbush and mmmmmmush

Match any one of the letters from A through M

[A-M]

Match ONE of a collection of characters

[pbm] as in /[pmb]ug/

Shortcut to search for [^0-9] anything that's not 0-9, (nondigit character)

\D

Non-whitespace shortcut (including \r\t\f\n\v)

\S

Shortcut to search the opposite of all alphanumeric characters.

\W (capital W)

Shortcut to match all digits. [0-9]

\d

Whitespace shortcut (including \r\t\f\n\v)

\s

Shortcut for [A-Za-z0-9_] (note that it includes underscore)

\w

Difference between x*, x+, and x?

x* = match something that has x zero or MORE times x? = match something that has x zero or ONE (not more) times x+ = match something that has x ONE or MORE times (not zero times)

Specify a character occurs between three and five times

x{3,5}

Specify a character occurs at least 3 times.

x{3,}

Specify a character occurs EXACTLY 3 times.

x{3}

What will this return in a match? /p(?=enguin)/

It will return p.

How do you specify lazy matching? Where does the lazy quantifier go?

Question mark, goes after * or +. If it went after a character (a letter) then it would mean match the character ZERO or ONE time.

How do you use the replace method and a regex to separate camel case words. For example letsHaveFun.

Match two capture groups and then separate them with .replace. For example /([a-z])([A-Z])1/g is the regex and the replace function is str.replace(regex, "$1 $2") which will put a space between the two capture groups.

Will /(?=\w{6,})(?=\d{2,}/ match bana12?

No. Lookaheads match from left to right starting at where you're at. This means that since nothing has been specified before the lookahead, it will start at the beginning of the string in both cases. The second lookahead will be false since it starts with a letter, not a number. To fix it you'd have to add \w* as in: /(?=\w{6,})(?=\w*\d{2,}/ (matches a word that's at least 6 characters long AND has two consecutive digits)

How does Regex really work? Does it go term by term and match it one by one?

The way to think of regex is to think of it as matching the entire Regex. If that match is in there by itself, then it'll return it. Yes, it tests it one by one, but that can be a really misleading way to think of it. It must match the entire group.

Flag to match all existing matches

g as in /test/g

Flag to ignore case

i

Specify a positive and negative lookaheads.

positive (?=phrasetomatch) negative (?!phrasetomatch)


Kaugnay na mga set ng pag-aaral

Anesthesia and physiological monitoring 14

View Set

Principles of Accounting I For Baddies

View Set

Module 5.2 CELL MEMBRANES-STRUCTURE AND TRANSPORT

View Set

Passpoint - Medication and I.V. Administration

View Set

Perception Chapter 10: The Visual System: Cortical Processing and Object Perception

View Set