Reputation: 15
I have this in javascript:
let text1 = 'Hello World by artist XXXX';
let text2 = 'Bye World by artist YYYY';
let text3 = '¿World? by artist ZZZZ';
I need to keep the text before "by artist XXXX" in another variable. Like this:
let keepText1 = 'Hello World';
let keepText2 = 'Bye World';
let keepText3 = '¿World?';
Is there a way to do it?
And How can I erase a phrase after the text "by" ?? I mean, what if the artist have a different name like:
let text4 = 'Hello Universe by artist Daniel Zamoga';
I need:
let keepText4 = 'Hello Universe';
Upvotes: 1
Views: 44
Reputation: 2804
you could split and take first part:
"Hello World by artist XXXX".split(" by artist")[0]
would return "Hello World"
Upvotes: 0
Reputation: 21638
Use a regex replace .replace(/ by artist [A-Z]+/, '')
console.log('Hello World by artist XXXX'.replace(/ by artist [A-Z]+/, ''));
console.log('Bye World by artist YYYY'.replace(/ by artist [A-Z]+/, ''));
console.log('¿World? by artist ZZZZ'.replace(/ by artist [A-Z]+/, ''));
Upvotes: 1