I want to replace all symbols that aren’t letters by -, but my code doesn’t work :
$reg = '/[^a-zA-Z]+/g';
$txt = $txt.replace($reg, '-');
What am I doing wrong?
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.
You need to un-quote the regex string so it’s treated as a regular expression literal, so you get this:
Regular expressions in JavaScript don’t need to be quoted as strings unless using the
new Regexp()notation; in the above example, it is now a regular expression literal, which isn’t treated as a string but a piece of regex to be used in.replace().