Replace values between parentheses using Javascript and Regex [duplicate] - javascript

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

Related

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

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

How to replace all / with - from string [duplicate]

This question already has answers here:
Replace forward slash "/ " character in JavaScript string?
(9 answers)
Why this javascript regex doesn't work?
(1 answer)
Closed 4 years ago.
I have a string field 01/01/1986 and I am using replace method to replace all occurrence of / with -
var test= '01/01/1986';
test.replace('//g','-')
but it does't give desire result. Any pointer would be helpful.
You just have a couple issues: don't put the regex in quotes. That turns it into a string instead of a regex and looks for that literal string. Then use \/ to escape the /:
var test= '01/01/1986';
console.log(test.replace(/\//g,'-'))
A quick way is to use split and join.
var test= '01/01/1986';
var result = test.split('/').join('-');
console.log(result);
Note too that you need to save the result. The original string itself will never be modified.

How to remove set of string using regex [duplicate]

This question already has answers here:
How can I delete a query string parameter in JavaScript?
(27 answers)
Closed 5 years ago.
On the following string I want to remove sort=*. I can get this done by run loop over it But want to get it done using regex which is new to me.
So start point should be "sort" and end point should be '&' and sort value could be *
I have created this one but dont how to add end point
string.replace((sort=*)\w,'')
"category_id=249&sort=name&category_filter=15&"
to
"category_id=249&category_filter=15&"
You ca use this regex instead (sort=.*?&), which can match every thing from sort= until the first occurrence of &
regex demo
Output
category_id=249&category_filter=15&

Remove Substring in Javascript [duplicate]

This question already has answers here:
How to remove text from a string?
(16 answers)
Closed 7 years ago.
In JavaScript, I need to find a substring between &J= and Key using regex and remove it from my url which could contain several substrings.
Here is my url:
SID=18608202881669&Act=432&Mode=1&CLk=T&Key58=6003&dotnetdll=TopCoConfigurator.dll&dotnetfunc=CasingSizeSummary&SID=18608202881669&F=&J=CasingSize/CasingSizeSummary.asp&Key58=6003&ccs_casingsizeid=6003
Thanks
You can learn Regular Expression here and code for your problem is:
Here you go:
SID="18608202881669&Act=432&Mode=1&CLk=T&Key58=6003&dotnetdll=TopCoConfigurator.dll&dotnetfunc=CasingSizeSummary&SID=18608202881669&F=&J=CasingSize/CasingSizeSummary.asp&Key58=6003&ccs_casingsizeid=6003";
var re = /&J=(.*?)&Key/;
SID = SID.replace(re, '');
alert(SID);
See Fiddle

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