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'
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:
Replace method doesn't work
(4 answers)
Closed 4 years ago.
I am trying to replace a single dash '-' character in a string with double dashes.
2015–09–01T16:00:00.000Z
to be
2015-–09-–01T16:00:00.000Z
This is the code I am using but it doesn't seem to be working:
var temp = '2015–09–01T16:00:00.000Z'
temp.replace(/-/g,'--')
In JavaScript Strings are immutable. So, when you modify a string, a new string object will be created with the modification.
In your case, the replace has replaced the characters but returns a new string. You need to store that in a variable to use it.
For example,
var temp = '2015–09–01T16:00:00.000Z';
temp = temp.replace(/–/g,'--');
Note The string which you have shown in the question, when copied, I realised that it is a different character but looks similar to – and it is not the same as hyphen (-). The character codes for those characters are as follows
console.log('–'.charCodeAt(0));
// 8211: en dash
console.log('-'.charCodeAt(0));
// 45: hyphen
The hyphen character – you have in the string is different from the one you have in the RegExp -. Even though they look alike, they are different characters.
The correct RegExp in this case is temp.replace(/–/g,'--')
Probably the easiest thing would be to just use split and join.
var temp = '2015–09–01T16:00:00.000Z'.split("-").join("--");
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 ).
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, '###');
});
This question already has answers here:
Are double and single quotes interchangeable in JavaScript?
(23 answers)
Closed 8 years ago.
Till now whatever programming languages I have encountered use double quotes for string and single quotes for characters but some of the javascript code that I saw seemed to be using them interchangeably,is it possible?
In which situations can they be interchanged?
In which situations they CANNOT be interchanged?
They are interchangeable, but when you open a string with one, you must close it with the same character. For instance,
"Hello' would not be a valid string.
EDIT: PS, this allows for you to enclose quotes in a string. For example, if you want to have the string literal "Hello", you could have
var string = '"Hello"'
yes they can as long as you keep in mind a few main things;
1- double quotes are the preferred by the browser to se they parameters values, so you never use them when setting javascript values or function calls within the DOM.
2- double quotes are also used to specify json an properties a json string using single quotes to delimiter they key, value pairs is not valid.
3-they don't complement each other, so what one starts one must end.
4 - if you need to use the same quote inside your string you need to scape it.