I am lost about how this works:
x=x.replace(/^\s+|\s+$/g,"");
What is the pipe( | ) for ?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It means
or. The part to the left matches any leading spaces (^), the part to the right matches any trailing space ($). Thegmodifier allows this matching to be applied more than once, which is useful if you’re expecting both trailing and leading space.Basically this regex trims whitespace.
An alternative way to write this regex is, using the
new RegExpconstruct:If find this notation more readable because you don’t need your delimiters (
/) and your modifier is separated.