ketan
ketan

Reputation: 19341

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);

Upvotes: 0

Views: 43

Answers (3)

Carlos1232
Carlos1232

Reputation: 815

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,''));

Upvotes: 2

doc_id
doc_id

Reputation: 1433

Escape the ^ as in:

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

The ^ means something in regex and must be escaped if you want it to be treated as normal character

Upvotes: 0

namgold
namgold

Reputation: 1070

Try this:

str = "[1fe3-46675-be1a-cd97084b]^Some Text@ dsd dsds [4j34-46675-be1a-cd97854b]^Another Text@";
replacedStr = str.replace(/\^[^@]*\@/g, '');
console.log(replacedStr)

Upvotes: 1

Related Questions