This question already has answers here:
Find comma in quotes with regex and replace with HTML equiv
(4 answers)
Closed 8 years ago.
In the below string,
'This "is, "just, for", Test", ignore it. My name is "FirstName, LastName".'
I want to replace all Commas(,) only inside the double quotes("") with ###.
For now I only found the matching pattern for (""), but need to build the regex to replace the commas.
/".*?"/g
Could you please help me? Thanks in advance ;)
Expected o/p: This "is### "just### for"### Test", ignore it. My name is "FirstName### LastName".
Note: This is not dupe of "Find comma in quotes with regex and replace with HTML equiv". Please see my expected o/p(Even I wanna replace the Comma in inner double quotes).
You can do this using a callback ...
var r = s.replace(/"[^"]+"/g, function(v) {
return v.replace(/,/g, '###');
});
Related
This question already has answers here:
Getting content between curly braces in JavaScript with regex
(5 answers)
Closed 2 years ago.
I need to match a specific regex syntax and split them so that we can match them to an equivalent value from a dictionary.
Input:
{Expr "string"}
{Expr "string"}{Expr}
Current code:
value.match(/\{.*\}$/g)
Desired Output:
[{Expr "string"}]
[{Expr "string"},{Expr}]
Use a non-greedy quantifier .*?. And don't use $, because that forces it to match all the way to the end of the string.
value = '{Expr "string"}{Expr}'
console.log(value.match(/\{.*?\}/g));
One option, assuming your version of JavaScript support it, would be to split the input on the following regex pattern:
(?<=\})(?=\{)
This says to split at each }{ junction between two terms.
var input = "{Expr \"string\"}{Expr}";
var parts = input.split(/(?<=\})(?=\{)/);
console.log(parts);
This question already has answers here:
regular expression add double quotes around values and keys in javascript
(4 answers)
Closed 2 years ago.
I have this string
coordinateid: [20,54.1],
colorid: [250,0,0],
sizeid: [2000],
tooltipid: [B],
How to get this result, adding quotes to the value of tooltipid only, leaving everything else as it is, I am using regex in javascript
coordinateid: [20,54.1],
colorid: [250,0,0],
sizeid: [2000],
tooltipid: ['B'],
You should match tooltipid in order to prevent adding quotes to other pairs.
This regex would do it: /tooltipid: \[(.*)\]/gm. And your replacement string should be tooltipid: ['$1'].
In JS the code would be: "the text".replace(/tooltipid: \[(.*)\]/gm, "tooltipid: ['$1']")
Here is the Regex demo: https://regex101.com/r/itwoYw/1.
If you're just trying to replace any value that's letters inside square brackets, replace \[([a-zA-Z]+)\] with ['$1'], where $1 is your first capture group.
Alternatively, you could use lookarounds and replace (?<=\[)([a-zA-Z]+)(?=]) with just '$1'
This question already has answers here:
Replace multiple characters in one replace call
(21 answers)
Closed 4 years ago.
I am trying to replace star (*) and colon (:) with an empty string ("") and the string can be as follows:
Either: Registration No: already exists*
OR: *Registration No: already exists
So, I don't want (*) as well as (:) and output should be Registration No already exists how can I solve it.
Trying as follows:
var txt = str.replace(/:\*/ig,"");
Please help me and thanks in advance
You regex matches :*. You could match either of them using a character class:
var txt = str.replace(/[:*]/g,"");
const strings = [
"Registration No: already exists*",
"*Registration No: already exists"
];
strings.forEach((s) => {
console.log(s.replace(/[:*]/g, ""));
});
Another way without using a character class is to use a pipe or alteration to separate each group. This however, requires that you escape special characters and this does allow for group matches instead of single character matches:
var txt = str.replace(/:|\*/ig, "");
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.
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 ).