I have a string that I am attempting to remove the word "and" from. I am attempting this by using the .replace method in javascript. I am able to remove the word "and" but the end result is not what I am expecting.
I would like my string to be returned similar to the console.log(testString)
where the entire string is returned as "i-have-a-test-test-test"
without any spacing in between. Currently, my attempt with console.log(newString)
returns the string without the -
in between each word.
My expected outcome is to have a return result as :
I-have-a-test-test-test
const string = "I have a test and test and test"
const newString = string.replace(/([^a-zA-Z0-9]*|\s*)\s\and/g, '-')
const testString = string.replace(/([^a-zA-Z0-9]*|\s*)\s/g, '-')
console.log(newString)
console.log(testString)