Skip to content

Remove string between special character JavaScript

I trying to remove some string from string which I have.

I have following string.

"[1fe3-46675-be1a-cd97084b]^Some Text@ dsd dsds [4j34-46675-be1a-cd97854b]^Another Text@"

I want to remove text between ^ @ including that character.

Output should be "[1fe3-46675-be1a-cd97084b] dsd dsds [4j34-46675-be1a-cd97854b]"

I used following but, not removing that string.

let str = "[1fe3-46675-be1a-cd97084b]^Some Text@ dsd dsds [4j34-46675-be1a-cd97854b]^Another Text@"

str = str.replace(/^.*@/g, '');

console.log(str);

Answer

You can do it with this regex.

let stringsS = "[1fe3-46675-be1a-cd97084b]^Some Text@ dsd dsds [4j34-46675-be1a-cd97854b]^Another Text@"
let regex = /^(.*?)@/gi

console.log(stringsS.replace(regex,''));