How to convert js string to rust - javascript

I would like to define a string from javascript and convert it to Rust.
In javascript, the string is defined as this:
var t = '5d.4n({"21":1u,"22":"25","26":"1u.2b","2l":2e,"2f":"2h","2j":["3-2k-2a.4.2","3-1t-1Y.4.2","3-1t-1J.4.2","3-1L-1M.4.2","3-1s-1O.4.2","3-1s-1R.4.2","3-1r-1T.4.2","3-1r-1W.4.2","3-1p-2m.4.2","3-1p-2N.4.2","3-1n-2R.4.2","3-1n-36.4.2","3-1l-2Z.4.2","3-1l-33.4.2","3-1j-2K.4.2","3-1j-2q.4.2","3-1g-2r.4.2","3-1g-2x.4.2","3-1e-2z.4.2","3-1e-2F.4.2","3-1y-2H.4.2","3-1y-2D.4.2","3-1z-2u.4.2","3-1z-30.4.2","3-1A-2Q.4.2","3-1A-28.4.2","3-1b-2W.4.2","3-1b-1H.4.2","3-o-1P.4.2","3-o-2M.4.2","3-q-2T.4.2","3-q-34.4.2","3-v-2J.4.2","3-v-2v.4.2","3-w-2C.4.2","3-w-2o.4.2","3-z-1E.4.2","3-z-37.4.2","3-B-2E.4.2","3-B-2B.4.2","3-k-2A.4.2","3-k-2y.4.2","3-6-2w.4.2","3-6-2t.4.2","3-9-2s.4.2","3-9-2p.4.2","3-b-2I.4.2","3-b-2L.4.2","3-f-35.4.2","3-f-32.4.2","3-h-31.4.2","3-h-2Y.4.2","3-U-2X.4.2","3-U-2U.4.2","3-19-2S.4.2","3-19-2P.4.2","3-16-2O.4.2","3-16-2n.4.2","3-14-2V.4.2","3-14-1V.4.2","3-11-1U.4.2","3-11-1S.4.2","3-Z-1Q.4.2","3-Z-1N.4.2","3-W-1X.4.2","3-W-1K.4.2","3-V-1I.4.2","3-V-1G.4.2","3-T-1F.4.2","3-T-1Z.4.2","3-Q-20.4.2","3-Q-2i.4.2","3-O-2g.4.2","3-O-2d.4.2","3-L-2c.4.2","3-L-29.4.2","3-J-27.4.2","3-J-24.4.2","3-G-23.4.2","3-G-2G.4.2","3-1c-3B.4.2","3-1c-39.4.2","3-H-4E.4.2","3-H-4D.4.2","3-I-4C.4.2","3-I-4B.4.2","3-K-4A.4.2","3-K-4z.4.2","3-M-4y.4.2","3-M-4x.4.2","3-N-4w.4.2","3-N-4v.4.2","3-P-4u.4.2","3-P-4t.4.2","3-R-4s.4.2","3-R-4F.4.2","3-S-4r.4.2","3-S-4p.4.2","3-5-4o.4.2","3-5-4m.4.2","3-1a-4l.4.2","3-1a-4k.4.2","3-X-4j.4.2","3-X-4i.4.2","3-Y-4h.4.2","3-Y-4g.4.2","3-10-4f.4.2","3-10-4e.4.2","3-12-4H.4.2","3-12-4q.4.2","3-13-4G.4.2","3-13-4U.4.2","3-15-4I.4.2","3-15-4Z.4.2","3-17-50.4.2","3-17-51.4.2","3-18-52.4.2","3-18-53.4.2","3-E-56.4.2","3-E-55.4.2","3-D-57.4.2","3-D-58.4.2","3-j-59.4.2","3-j-5a.4.2","3-g-5b.4.2","3-g-5c.4.2","3-d-4Y.4.2","3-d-54.4.2","3-c-4W.4.2","3-c-4V.4.2","3-a-4X.4.2","3-a-4T.4.2","3-8-4S.4.2","3-8-4R.4.2","3-7-4Q.4.2","3-7-4P.4.2","3-i-4O.4.2","3-i-4N.4.2","3-u-4M.4.2","3-u-4L.4.2","3-l-4K.4.2","3-l-4J.4.2","3-A-4c.4.2","3-A-4d.4.2","3-y-4a.4.2","3-y-49.4.2","3-x-4b.4.2","3-x-3A.4.2","3-C-3z.4.2","3-C-3y.4.2","3-t-3x.4.2","3-t-3w.4.2","3-r-3v.4.2","3-r-3u.4.2","3-p-3t.4.2","3-p-3s.4.2","3-n-3r.4.2","3-n-3q.4.2","3-F-3p.4.2","3-F-3C.4.2","3-1d-3o.4.2","3-1d-3m.4.2","3-1D-3l.4.2","3-1D-3k.4.2","3-1B-3j.4.2","3-1B-3i.4.2","3-1C-3h.4.2","3-1C-3g.4.2","3-1w-3f.4.2","3-1w-3e.4.2","3-1v-3d.4.2","3-1v-3c.4.2","3-1f-3b.4.2","3-1f-3a.4.2","3-1h-3n.4.2","3-1h-3D.4.2","3-1i-3E.4.2","3-1i-3F.4.2","3-1k-48.4.2","3-1k-47.4.2","3-1m-46.4.2","3-1m-45.4.2","3-1x-44.4.2","3-1x-43.4.2","3-1o-42.4.2","3-1o-41.4.2","3-1q-40.4.2","3-1q-3Z.4.2","3-3Y-3X.4.2"],"3W":3V,"3U":3T,"3S":"/3R/s/3Q/3P/3O/","3N":1,"3M":"","3L":3K,"3J":0,"3I":{"e":3H,"m":"3G"}}).38();'
However, I can not defined this string in rust like this:
let a:&str = "5d.4n({"21":1u,"22":"25","26":"1u.2b","2l":2e,"2f":"2h","2j":["3-2k-2a.4.2","3-1t-1Y.4.2","3-1t-1J.4.2","3-1L-1M.4.2","3-1s-1O.4.2","3-1s-1R.4.2","3-1r-1T.4.2","3-1r-1W.4.2","3-1p-2m.4.2","3-1p-2N.4.2","3-1n-2R.4.2","3-1n-36.4.2","3-1l-2Z.4.2","3-1l-33.4.2","3-1j-2K.4.2","3-1j-2q.4.2","3-1g-2r.4.2","3-1g-2x.4.2","3-1e-2z.4.2","3-1e-2F.4.2","3-1y-2H.4.2","3-1y-2D.4.2","3-1z-2u.4.2","3-1z-30.4.2","3-1A-2Q.4.2","3-1A-28.4.2","3-1b-2W.4.2","3-1b-1H.4.2","3-o-1P.4.2","3-o-2M.4.2","3-q-2T.4.2","3-q-34.4.2","3-v-2J.4.2","3-v-2v.4.2","3-w-2C.4.2","3-w-2o.4.2","3-z-1E.4.2","3-z-37.4.2","3-B-2E.4.2","3-B-2B.4.2","3-k-2A.4.2","3-k-2y.4.2","3-6-2w.4.2","3-6-2t.4.2","3-9-2s.4.2","3-9-2p.4.2","3-b-2I.4.2","3-b-2L.4.2","3-f-35.4.2","3-f-32.4.2","3-h-31.4.2","3-h-2Y.4.2","3-U-2X.4.2","3-U-2U.4.2","3-19-2S.4.2","3-19-2P.4.2","3-16-2O.4.2","3-16-2n.4.2","3-14-2V.4.2","3-14-1V.4.2","3-11-1U.4.2","3-11-1S.4.2","3-Z-1Q.4.2","3-Z-1N.4.2","3-W-1X.4.2","3-W-1K.4.2","3-V-1I.4.2","3-V-1G.4.2","3-T-1F.4.2","3-T-1Z.4.2","3-Q-20.4.2","3-Q-2i.4.2","3-O-2g.4.2","3-O-2d.4.2","3-L-2c.4.2","3-L-29.4.2","3-J-27.4.2","3-J-24.4.2","3-G-23.4.2","3-G-2G.4.2","3-1c-3B.4.2","3-1c-39.4.2","3-H-4E.4.2","3-H-4D.4.2","3-I-4C.4.2","3-I-4B.4.2","3-K-4A.4.2","3-K-4z.4.2","3-M-4y.4.2","3-M-4x.4.2","3-N-4w.4.2","3-N-4v.4.2","3-P-4u.4.2","3-P-4t.4.2","3-R-4s.4.2","3-R-4F.4.2","3-S-4r.4.2","3-S-4p.4.2","3-5-4o.4.2","3-5-4m.4.2","3-1a-4l.4.2","3-1a-4k.4.2","3-X-4j.4.2","3-X-4i.4.2","3-Y-4h.4.2","3-Y-4g.4.2","3-10-4f.4.2","3-10-4e.4.2","3-12-4H.4.2","3-12-4q.4.2","3-13-4G.4.2","3-13-4U.4.2","3-15-4I.4.2","3-15-4Z.4.2","3-17-50.4.2","3-17-51.4.2","3-18-52.4.2","3-18-53.4.2","3-E-56.4.2","3-E-55.4.2","3-D-57.4.2","3-D-58.4.2","3-j-59.4.2","3-j-5a.4.2","3-g-5b.4.2","3-g-5c.4.2","3-d-4Y.4.2","3-d-54.4.2","3-c-4W.4.2","3-c-4V.4.2","3-a-4X.4.2","3-a-4T.4.2","3-8-4S.4.2","3-8-4R.4.2","3-7-4Q.4.2","3-7-4P.4.2","3-i-4O.4.2","3-i-4N.4.2","3-u-4M.4.2","3-u-4L.4.2","3-l-4K.4.2","3-l-4J.4.2","3-A-4c.4.2","3-A-4d.4.2","3-y-4a.4.2","3-y-49.4.2","3-x-4b.4.2","3-x-3A.4.2","3-C-3z.4.2","3-C-3y.4.2","3-t-3x.4.2","3-t-3w.4.2","3-r-3v.4.2","3-r-3u.4.2","3-p-3t.4.2","3-p-3s.4.2","3-n-3r.4.2","3-n-3q.4.2","3-F-3p.4.2","3-F-3C.4.2","3-1d-3o.4.2","3-1d-3m.4.2","3-1D-3l.4.2","3-1D-3k.4.2","3-1B-3j.4.2","3-1B-3i.4.2","3-1C-3h.4.2","3-1C-3g.4.2","3-1w-3f.4.2","3-1w-3e.4.2","3-1v-3d.4.2","3-1v-3c.4.2","3-1f-3b.4.2","3-1f-3a.4.2","3-1h-3n.4.2","3-1h-3D.4.2","3-1i-3E.4.2","3-1i-3F.4.2","3-1k-48.4.2","3-1k-47.4.2","3-1m-46.4.2","3-1m-45.4.2","3-1x-44.4.2","3-1x-43.4.2","3-1o-42.4.2","3-1o-41.4.2","3-1q-40.4.2","3-1q-3Z.4.2","3-3Y-3X.4.2"],"3W":3V,"3U":3T,"3S":"/3R/s/3Q/3P/3O/","3N":1,"3M":"","3L":3K,"3J":0,"3I":{"e":3H,"m":"3G"}}).38();"
How can I define this string in Rust?

You can use rust raw strings by prepending r# and appending # to allow quotation marks within the string, as explained in the str literals-and-escapes documentation
let a = r#"5d.4n({"21":1u, ...})"#;

Related

Split String using backslash in javascript

This is my Temp url and I'm trying to get image name
var str='C:\fakepath\alfa_company.png';
my expected out put like this:
var url='alfa_company.png';
In Javascript "\" has a special meaning.
So, it doesn't get included in your resultant string.
Try
let u = String.raw`C:\fakepath\alfa_company.png`;
u.split("\\")[u.split("\\").length-1]
or
let u = String.raw`C:\fakepath\alfa_company.png`;
u.split("\\").pop()
to understand it better go through How can I use backslashes (\) in a string?
You don't need any jQuery for that :)
const path = 'C:\\fakepath\\alfa_company.png';
const filename = path.split('\\').pop(); // alfa_company.png
You need to use double backlashes because JavaScript treats them as escape characters.

How to replace a multiple patterns in the same string using regex and javascript?

i want to replace multiple patterns in the same string using regex and javascript.
What i am trying to do?
i have a string for example
string = "hello i am [12#fname lname] and i am referring this user [23#fname1 lname1]"
now i get all the strings with [] using regex
const get_strings_in_brackets = string.match(/\[(\d+#[\w\s]+)]/g);
so get_strings_in_brackets will have
["[12#fname lname]", "[23#fname1 lname1]"]
now i want these to be replaced with string "<some-tag id="12"/> "<some-tag id="23"/> in the string "hello i am [12#fname lname] and i am referring this user [23#fname1 lname1]"
also this number 12 in this string "<some-tag id="12"/> is got from the string ["[12#fname lname]" before # character.
What i have tried to do?
i have tried to replace for only one string withing brackets meaning for the example below
string ="hello i am [12#fname lname1]"
const extracted_string_in_brackets = string.match(/\[(\d+#[\w\s]+)]/g);
const get_number_before_at_char =
extracted_string_in_brackets[0].substring(1,
extracted_string_in_brackets[0].indexOf('#'));
const string_to_add_in_tag = `<some-tag
id="${get_number_before_at_char}"/>`;
const final_string = string.replace(extracted_string_in_brackets,
string_to_add_in_tag);
The above code works if i have only one string within square brackets. But how do i do it with multiple strings in brackets and replacing that with tag string that is for example .
Could someone help me solve this. thanks.
Just use a group reference in your replacement:
string = "hello i am [12#fname lname] and i am referring this user [23#fname1 lname1]"
newstr = string.replace(/\[(.+?)#(.+?)\]/g, '<some-tag id="$1"/>')
console.log(newstr)

JS - parsing data with random single quotes

How do I parse an array of objects if it contains single quote characters?
For example, I have:
$example = '{"response":[{"uid":3202935,"first_name":"Martha","last_name":"O'Nill","user_id":3202935},{"uid":4070530,"first_name":"Alex","last_name":"White","user_id":4070530}]}';
The single quotes seem to break up the array, making parsing impossible.
You can use backticks (``). It will generate the string as it was written, with double "" and single ' quotes.
var str = `{"response":[{"uid":3202935,"first_name":"Martha","last_name":"O'Nill","user_id":3202935},{"uid":4070530,"first_name":"Alex","last_name":"White","user_id":4070530}]}`;
console.log(str);
var obj = JSON.parse(str);
console.log(obj.response[0].uid);
It's a json string not an object.
Use JSON.parse(myJsonSting) and you will get objects with the ' taken care of.
Javascript is supposed to ignore single quotes if it is inside double quotes, in your case try adding backslash before the single quote.
To parse at json string to object
var example = '{"response":[{"uid":3202935,"first_name":"Martha","last_name":"O'Nill","user_id":3202935},{"uid":4070530,"first_name":"Alex","last_name":"White","user_id":4070530}]}';
var objExample = JSON.parse(example);
To convert json object to json string
var StrExample = JSON.stringify(objExample);

Change javascript regex divider character

I have a simple JS question.
I have this code, and what I need is cut the textbox value every two characters (this works fine), but I want to change the comma with the column.
My actual result is:
stringtest - st,ri,ng,te,st
and I want this:
stringtest - st:ri:ng:te:st
my code is:
function test() {
var textboxtext= $("#textbox").val();
var splitted = textboxtext.match(/.{2}|.{1,2}/g);
alert("B8:27:EB:" + splitted)
The problem is not with the regex, but with how you're converting the result array to a string. When the JavaScript engine needs to convert an array to a string (which is done implicitly when you use the binary + operator with an string on either side), it calls the toString() method, which basically just calls the join() method, which returns a string with each element of the array converted to a string, and separated by commas.
But you can call the join method yourself and specify what character you'd like it to use as a separator, like this:
alert("B8:27:EB:" + splitted.join(':'));
On a side note, you can simplify your regex to .{1,2}, which is exactly the same as what you had previously:
var splitted = textboxtext.match(/.{1,2}/g);

Replace wildcard string inside double quote

I have this string
var newtypegraph=newtypegraph.replace(' "type":"bar3d" ',' "type":"area" ');
I want to make bar3d as wildcard.
This looks like JavaScript, so you can use regex:
var newtypegraph = newtypegraph.replace(/"type":".*?"/g, '"type":"area"');
This looks like the properties of a JSON object. Is this JSON?

Categories