Replace pattern within string with space [duplicate] - javascript

This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 7 years ago.
I am getting long string with multiple occurances of pattern './.'. The string has dates as well in a format of dd.mm.yyyy.
First I tried with javascript replace method as:
str.replace('./.', ''). But it replaced only first occurance of './.'
Then I tried another regex which replaces special characters but it didn't work as it replaced '.' within dates as well.
How do I replace multiple occurances of a pattern './.' without affecting any other characters of a string ?

. is a special character in a regexp, it matches any character, you have to escape it.
str.replace(/\.\/\./g, '');

Use this simple pattern:
/\.\/\./g
to find all "./." strings in your text.

Try it :
str.replace(/\.\/\./g, '');

Escape . and d \
Add a g for global
Like this
str = str.replace(/\./\./g, '');

Related

Replace any of several characters with underscore [duplicate]

This question already has answers here:
Replace special characters in a string with _ (underscore)
(2 answers)
Closed 1 year ago.
I have a simple task but I'm not sure of the syntax.
I have a string and want to replace any occurrences of '[', ']', or '.' with an underscore ('_').
I know that string.replace() supports regular expressions, which also give special treatment to [ and ].
Use replaceAll for that
** Note, replace will also work since this a global search.
const src = '/[[\].]/g';
const target = '_';
const formated = string.replaceAll(src, target);
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll
Escape the characters with special treatment with backslash.
string = string.replace(/[[\].]/g, '_');
Note that [ and . don't receive special treatment inside [].

How can I replace every regex match with itself and some concatenations? [duplicate]

This question already has answers here:
Using $0 to refer to entire match in Javascript's String.replace
(2 answers)
Closed 3 years ago.
I have a console output that is a string {x:0,y:0,width:1920,height:1080} and need to convert it to object but I can't JSON.parse() it until all properties are surounded by quotes.
I managed to find this regex expression that will match with any word: \b[\w]+\b but I don't know how to use every match to replace '"' + match + '"' on both sides. I realized there are also numbers in there so maybe this would be a better regex: \b[a-zA-Z]+\b provided that property names never include numbers.
Use a group (i.e.: enclose the pattern with ( and )) and access it with $1:
var out = "{x:0,y:0,WIDTH:1920,hEiGhT:1080}";
var rgx = /\b([a-z]+)\b/gi; // use the flag 'i' to make it case-insensitive
console.log(out.replace(rgx, '"$1"'));

How to replace brackets in string using regex in node js? [duplicate]

This question already has an answer here:
Parenthesis not being replaced in regex
(1 answer)
Closed 4 years ago.
I'd like to remove all occurences of "(" and ")" in a string but the following replace line is throwing up a 'group not terminated' error.
str = "1+((x*(2*3))+10)";
console.log(str.replace(//(/gi,"");
How should I do this?
A '(' character has special meaning in RegEx (start of Group), you must escape the parentese like this:
\(
The same for an end parentese. Alternatively you can use a character Group like this:
[()]+
That will select any character in the Group (in this case parenteses) on or more times.
Try replacing a character class containing both opening and closing parentheses:
var str = "1+((x*(2*3))+10)";
console.log(str.replace(/[()]+/gi,""));
1+x*2*3+10
But, it is not clear why you would want to do this, because most likely removing all parentheses would change the value of the expression.

Replace a string containing special characters [duplicate]

This question already has answers here:
What special characters must be escaped in regular expressions?
(13 answers)
Closed 4 years ago.
Trying to replace a string that contains special characters. The purpose of this is to convert the query string into an understandable format for end users.
full string is:
var str = 'active=true^opened_by=6816f79cc0a8016401c5a33be04be441^ORassigned_to!=6816f79cc0a8016401c5a33be04be441^short_descriptionISNOTEMPTY^NQopened_atONToday#javascript:gs.beginningOfToday()#javascript:gs.endOfToday()^EQ';
Specifically the portion after ^NQ, in this example: opened_atONToday#javascript:gs.beginningOfToday()#javascript:gs.endOfToday(). I have split the original string with indexOf(^NQ) and passing the resulting sub-strings to a function. I'm then trying a .replace() as below:
var today = replacementString.replace(/(ONToday#javascript:gs.beginningOfToday()#javascript:gs.endOfToday())/g, ' is today ');
replacementString = today;
I have tried with various combinations of the above line, but have not returned what I am hoping for.
I've had no issues replacing special characters, or strings without special characters, but the combination of the 2 is confusing/frustrating me.
Any suggestions or guidance would be appreciated
You should escape the () to \(\) to match it literally or else it would mean a capturing group. For the match you could also omit the outer parenthesis and you have to escape the dot \. to match it literally.
ONToday#javascript:gs\.beginningOfToday\(\)#javascript:gs\.endOfToday\(\)
var str = 'active=true^opened_by=6816f79cc0a8016401c5a33be04be441^ORassigned_to!=6816f79cc0a8016401c5a33be04be441^short_descriptionISNOTEMPTY^NQopened_atONToday#javascript:gs.beginningOfToday()#javascript:gs.endOfToday()^EQ';
var today = str.replace(/ONToday#javascript:gs\.beginningOfToday\(\)#javascript:gs\.endOfToday\(\)/g, ' is today ');
replacementString = today;
console.log(today);

How do I replace all string values in this JavaScript statement? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
replace all occurrences in a string
I have this string:
"12-3-02"
And I would like to convert it to :
"12/3/02"
How would I do this? I 've tried :
.replace(/-/,"/")
But this only replaces the first one it finds. How do I replace all instances?
One of these (using split, or a global RegEx flag):
str = str.split('-').join('/'); // No RegExp needed
str = str.replace(/-/g, '/'); // `g` (global) has to be added.
Add the g (global) modifier to the regex, to match all -s.
.replace(/-/g,"/")
If you want to replace all the occurences of - by / then use this where g specifies the global modifier.
"12-3-02".replace(/-/g,"/");
Working demo - http://jsfiddle.net/ShankarSangoli/QvbM8/
Try with:
"12-3-02".replace(/-/g, '/');
This question has been posed about a thousand times, but no one has ever considered the possibility that, in the real world, characters can occur in places where you might not expect.(Typo's in input or replacing parts of words when you only wanted to replace a single word...
var reformat = '01-11-2012'.replace(/(([0-9]+)(?=\-))\-(?=[0-9])/g,'$1/');
This will only replace '-' characters that are preceded and followed by a number.

Categories