1

I have a problem, i have a string like:

<div dir="ltr">adasdsad<div>dsadsadasd</div><div>sadasdsad</div></div>

and I want to inset > after <div> or <dir="ltr">. So result will be:

<div dir="ltr">>adasdsad<div>>dsadsadasd</div><div>>sadasdsad</div></div>

Please help !

1 Answer 1

1

You can use .replace() with a regular expression of /(\<div.*?\>)/g to group all divs with any attributes (.*?), and then replace it with the captured group followed by a > by using '$1>' as the replacement argument

See example below:

const str = '<div dir="ltr">adasdsad<div>dsadsadasd</div><div>sadasdsad</div></div>';

const res = str.replace(/(\<div.*?\>)/g, '$1>');
console.log(res);

Sign up to request clarification or add additional context in comments.

1 Comment

@NguyễnVŨ no worries ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.