I'm trying to replace a value in a string with a double curly (used by postman for variable substitution), but every time I try to quote or escape the braces, I always get additional escaped quotes or double escaped braces, all of which break the substitution:
Original String:
"header": [{"key": "x-device-auth","value": "\"token\""}]
OriginalString.replace('token','{{token}}')
Result:
"header":[{"key":"x-device-auth","value":"\"{{token}}\""}]
If I search for .replace('\"token\"','{{token}}'), I don't get a match. The final string needs to be:
"header": [{"key": "x-device-auth","value": "{{token}}"}]
You should be looking for token with the escaped wrapping double quotes, since you also want to replace those.
var originalString = '"header": [{"key": "x-device-auth","value": "\\"token\\""}]';
console.log(originalString);
console.log(originalString.replace('\\"token\\"','{{token}}'));
originalString = '"header": [{"key": "x-device-auth","value": "\"token\""}]';
console.log(originalString);
console.log(originalString.replace('"token"','{{token}}'));
I have added two cases, one with the original string actually containing backslashes (first originalstring definition). The second without. Choose the one, that best matches your actual input :-)
I dont see the input string is having proper escape characters applied. As you posted question in javascript tag, I tried below with javascript and its giving required results.
var str = "\"header\": [[{\"key\": \"x-device-auth\",\"value\": \"token\"}]";
var res = str.replace('token','{{token}}');
Related
I'm trying to format a JSON input string in Javascript so that I can use it as a map.
{accountNumber:E22E6178D16777E1E053020011AC64B0,paymentMethodObject:<ns2:Token>123</ns2:Token><ns2:Type>CreditCard</ns2:Type>,preview:true}
To do this I need to put quotation marks around each key and value. Like here:
{
"accountNumber": "12345",
"paymentMethodObject": "<ns2:Token>123</ns2:Token><ns2:Type>CreditCard</ns2:Type>",
"preview": "true"
}
The problem is when I try to do it, quotes get added to the XML values also because they also contain a colon.
Maybe you need to use method replace with pattern Regex that match key:value replace it with surrounded by quotes pair!
let strJSON = '{accountNumber:E22E6178D16777E1E053020011AC64B0,paymentMethodObject:<ns2:Token>123</ns2:Token><ns2:Type>CreditCard</ns2:Type>,preview:true}';
let objJSON = strJSON.replace(/(\w+):([\/<>:\w+]+)/g,'"$1":"$2"');
console.log(objJSON)
I have a string that has some double quotes escaped and some not escaped.
Like this,
var a = "abcd\\\"\""
a = a.replace(/\[^\\\]\"/g, 'bcde')
console.log(a)
The string translates to literal, abcd\"".
Now, i am using the above regex to replace non-escaped double quotes.
And only the second double quote must be replaced.
The result must look like this,
abcd\"bcde
But it is returing the same original string, abcd\"" with no replacement.
You can use capture group here:
a = a.replace(/(^|[^\\])"/g, '$1bcde')
//=> abcd\"bcde
A negative lookbehind is what you want. However it is not supported in the Regex' JS flavor.
You can achieve this by processing the result in two steps:
var a = "abcd\\\"\"";
console.log(a);
var result = a.replace(/(\\)?"/g, function($0,$1){ return $1?$0:'{REMOVED}';});
console.log(result);
If I want to replace a digit in the string, I would do the following:
"a3v".replace(/\d+/,"") // "av"
However, in the string "dynamic_fields[n][key]", I want to replace the n inside the brackets with 1. This is what I have done so far:
"dynamic_fields[n][key]".replace(/^.+[(n)]/,1)
Unfortunately, this is the result it gave me:
"1][key]"
Even though I expected:
"dynamic_fields[1][key]"
How come it doesn't recognize the capturing group () and replace the content in it with 1? What am I doing wrong?
This could be easily done like this:
var string = "dynamic_fields[n][key]";
var replaced = string.replace(/\[n\]/,"\[1\]");
alert(replaced);
Basically, this finds the character "n" that is surrounded with brackets, and replaces it with a "1" surrounded with brackets. Remeber to "escape" the brackets to make them literal. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Values,_variables,_and_literals#String_literals
I hope this helps!
You have to change it to be like that
"dynamic_fields[n][key]".replace(/[(n)]/g,1);
here's the sample running one
http://ideone.com/dzFGqQ
Hi I want to escape single quote within another string.
I have the following string:
'I'm a javascript programmer'
In the above string , I need to escape single quote
and the expected output is:
'I\'m a javascript programmer'
I required this to handle in eval() in javascript.
The String would be like this...
"[['string's one','string two','string's three']]"
How to solve this. Thanks in advance...
This can do the trick:
var str = "'I'm a js programer.'";
str.replace(/(\w)'(\w)/g, "$1\\\'$2");
var s = "my string's"
s = s.replace(/'/g, "\\'");
The proper way to escape quotes in an html string would be with a character entity.
'I'm a javascript programmer'
The same goes for double quotes:
'"I'm a javascript programmer"'
You can try lookahead assertions to achieve the desired effect:
var str = "'I'm a javascript's programmer'";
str = str.replace(/(?!^)'(?!$)/g, "\\'");
(Fiddle). Unlike Shimon's answer, this can also deal with double single quotes ('').
Negative lookahead assertion (?! )doesn't do any matching by itself but it ensures that the asserted expression doesn't occur at the given position (i.e. start of string ^ doesn't occur before the quote and end of string $ doesn't occur after the quote).
I'm trying to make an auto-complete function for twitter usernames.
So far, I have the following code:
function OnKeyUp(txtboxid){
var text = $('#'+txtboxid).val()
var regex = '(^|\s)#(\w*[a-zA-Z_]+\w*)'
var results = text.match(RegExp(regex, 'gm'))
console.debug(results)
}
The problem is, it matches only text when it is at the beginning of the string (eg: #yser)
What i want is a regex that can mach such a string like this "hello #user2 , #user and #user3 how are you"
I'm not sure how to accomplish this.
Searched google for about 3 hours now and still nothing found.
Also, it would be great to only the the last username when its changed.
Your regex is fine. The only problem is that backslashes in the string will be removed or replaced when the string is parsed, instead of being interpreted by the regular expression parser. You need to re-escape each of them with an extra backslash:
var regex = '(^|\\s)#(\\w*[a-zA-Z_]+\\w*)';
Instead of specifying the regular expression with a string and the RegEx function, you should usually use a regular expression literal. It's delimited by backslashes instead of double-quotes, with the flags appended to the end:
var results = text.match(/(^|\s)#(\w*[a-zA-Z_]+\w*)/gm);