regex start line 1

regex start line

// Match from the start of the line
^
//example:
let str = "HELLO hello world"
let reg = /hello/g //g option: doesn't stop at first match
str.match(reg) // Array(2)["HELLO", "hello"]
reg = /^hello/g //match hello at the start of the string
str.match(reg) // Array(1)["HELLO"]

Here is what the above code is Doing:
1. The ^ character is used to match the start of the string.
2. The g option is used to perform a global match (find all matches rather than stopping after the first match).
3. The regex /^hello/ matches the string “hello” at the start of the string.
4. The string “hello” is found at the beginning of the string, so it is returned as part of the resulting array.
5. The string “hello” is also found in the string “HELLO”, but without the g option, it would stop searching at the first match, and return [“HELLO”]

Similar Posts