How to get rid of everything inside <> brackets in Javascript? [duplicate] - javascript

This question already has answers here:
Strip HTML from Text JavaScript
(44 answers)
Closed 4 years ago.
I have a text that looks like
<00:02:00.820><c> ему</c><00:02:00.970><c> кто</c></c><c.colorE5E5E5><00:02:01.180><c> пойдет</c></c>
I need to get rid of everything that is inside the <> tags, so the result looks like:
ему кто пойдёт
How would I do that using Javascript? Would regex like this work:
string.replace([<^\>]*],'');

You need to pass either a string or a regular expression literal into .replace's first argument; .replace([<^\>]*], isn't valid syntax. I'd prefer to lazy-repeat any character until getting to another >, with <.*?>:
const str = '<00:02:00.820><c> ему</c><00:02:00.970><c> кто</c></c><c.colorE5E5E5><00:02:01.180><c> пойдет</c></c>';
console.log(
str.replace(/<.*?>/g, '')
);

Related

How to find out if a string contains all characters in a set of characters [duplicate]

This question already has answers here:
Test If String Contains All Characters That Make Up Another String
(4 answers)
Closed 2 years ago.
I have been struggling with a regex pattern to find out if a string contains all characters in a set of characters.
e.g I want to test if "sheena" contains esnh, I should get a match but if I want to test if "sheena" contains esnk, I should not get a match because there exist no k in "sheena" even thought esn exists.
To solve this problem just use the every method.
const matchString = (str, matcher) => [...matcher].every(n => str.includes(n));
console.log(matchString('sheena', 'eshn'));
console.log(matchString('sheena', 'eshk'));

How to give input to replace() when the string is present in an array [duplicate]

This question already has answers here:
Loop (for each) over an array in JavaScript
(40 answers)
Closed 3 years ago.
I have to replace the special characters in my string with underscore. replace() function is used to do it . I know that.
My string is "545123_Claims#Claims#Claims000117".
But the issue is that replace() accepts sting as input.
Actually my string is in an array like filArr= ["545123_Claims#Claims#Claims000117"].
So how can I replace the special character in thisstring which is inside an array?
You could map the replaced strings by taking a function.
const replacementFn = string => string.replace(/xxx/, 'yyy');
filArr = fillArr.map(replacementFn);

Match text in nested parenthesis [duplicate]

This question already has answers here:
Regular Expression to get a string between parentheses in Javascript
(10 answers)
How to match a input parenthesis with regular expression in JavaScript?
(4 answers)
Closed 4 years ago.
As the title says, I cant figure out how to match text inside a nested parenthesis (only in nested parenthesis). E.g
I have this text Hi my (name is (Jhon) and(i dont) know how (to)match) this.
The text I need to match is Jhon, i dont and to.
But text like this one hi i have (a parenthesis) should not match anything
Every answer i've seen until now uses recursive regex but I can not use a recursive regex in js and also I dont need a dinamic nested level identifier, just the text (whatever text) is inside the second parenthesis. ((this text))
I've tried (with no success) this {({([^{}])*})*}
This is not a dupe since The guy at the other question just ask for a single parenthesis....

Replace values between parentheses using Javascript and Regex [duplicate]

This question already has answers here:
How can I replace a string in parentheses using a regex?
(4 answers)
Closed 7 years ago.
I need to replace the text between two parentheses using Regex in Javascript. For example:
var x = "I need to go (now)";
I need to replace 'now' with 'tomorrow'. I tried this, but it didn't work:
x.replace(/\(now)\b/g, 'tomorrow');
"I need to know (now)".replace(/\(now\)/g, 'tomorrow');
You don't need the \b and you need to escape the second ).

Replace string including asterisk in Javascript [duplicate]

This question already has answers here:
How do I replace an asterisk in Javascript using replace()?
(6 answers)
Closed 7 years ago.
I am trying to replace the same string *a*a consistently with *a.
Tried many variations of something like this, but none really worked:
s = s.replace( /\b*a*a\b/g, "*a");
So far running this leads to all xzy*a being replaced with xyz
* is a special regex character. If you want to match only an actual asterisk, then you have to escape it like this:
s = s.replace( /\*a\*a/g, "*a");
Working demo: http://jsfiddle.net/jfriend00/gvgshwyz/
An asterisk is a special regex character.
You just have to escape it like this: \*a in place of *a

Categories