Is there a way to pass a string to be interpolated to Javascript coming from Python?
For example, to string interpolate a Python string, I f-strings like so
>>> friend = "Bob"
>>> f"Hey there, {friend}"
Hey there, Bob
However, I want to send a string that the client (Javascript code) can also string interpolate.
friend1 = "Bob"
friend2 = "Jill"
f"Hi here {friend}! When are you going to the {LOCATION}? {friend2} is going at 8AM."
In the example above, I only want to fill in the values for friend1 and friend2 but I want LOCATION to be filled in by the client.
replace seems to be the easiest option
const text = "Hi here Bob! When are you going to the {LOCATION}? Jill is going at 8AM."
const result = text.replace('{LOCATION}', 'cinema')
console.log(result)
Related
Basically, we have a .txt-file containing a table that is tab separated like the following one (___ represents a tab):
Name___Phone___City
Person1___111-111-1111___City1
Person2___222-222-2222___City2
We want to output another .txt-file that should contain the following:
Name, Person1, Person2
Phone, 111-111-1111, 222-222-2222
City, City1, City2
I tired using string.split() but couldn't produce the desired result. How can I do the above presented transformation with JavaScript?
Again no clue what language you're using, here's a one liner that works from any terminal as long as perl is installed (Unix based systems automatically, available on windows as well through download)
perl -pi -e 's/\t/, /g' /path/to/file
The first thing I would do is get the lines using this Regex /(.+)\n?/. Then, in each line, I'd split & join on the three underscores.
function reorder(input){
var regex = /(.+)\n?/gm, m, output = '';
while (m = regex.exec(input)) output += m[1].split('___').join(', ') + '\n';
return output;
};
Not sure you can write to a file in JavaScript though; maybe in Node.js?
What I am looking for is how strings are physically treated in Javascript. Best example I can think of for what I mean is that in the Java api it describes the storage of strings as:
String str = "abc";" is equivalent to: "char data[] = {'a', 'b', 'c'};
To me this says it uses an array object and stores each character as its own object to be used/accessed later (I am usually wrong on these things!)...
How does Javascript do this?
Strings are String objects in JavaScript. The String object can use the [] notation to get character from a string ("abc"[0] returns 'a'). You can also use the String.prototype.charAt function to achieve the same result.
Side node: var a = 'abc' and var b = new String('abc') are not the same. The first case is called a primitive string and get converted to a String object by the JavaScript parser. This results in other data types, calling typeof(a) gives you string but typeof(b) gives you object.
Strings are stored in the same format in javascript as other languages stores.
Suppose var word = "test" than at word will be as an array of characters and the 't' will come at 0th position and so on.
The last iteration as taking 'word.length' will return undefined. In other languages, it returns as '\0'.
Hey everyone quick question, I know this sounds strange to do in javascript but i have good use for it. I need to be able to parse a string passed in a textarea in such a way that escaped hex literals "\x41" or whatever are processed not as four chars '\' 'x' '4' '1' but as 'A' for example:
var anA = "\x41";
console.log(anA); //emits "A"
var stringToParse = $(#someTextArea).val(); //using jquery for ease not a req
//lets say that "someTextArea" contains "\x41"
console.log(stringToParse); // equals "\" "x" "4" "1" -- not what i want
console.log(new String(stringToParse)); same as last
console.log(""+stringToParse); still doesnt work
console.log(stringToParse.toString()); failz all over (same result)
I want to be able to have a way for stringToParse to contain "A" not "\x41"... any ideas beyond regex? I'll take a regex i guess, i just wanted a way to make javascript do my bidding :)
String.prototype.parseHex = function(){
return this.replace(/\\x([a-fA-F0-9]{2})/g, function(a,b){
return String.fromCharCode(parseInt(b,16));
});
};
and in practice:
var v = $('#foo').val();
console.log(v);
console.log(v.parseHex());
I figured it out although its kind of hacky and i use eval :(... if anyone has a better way let me know:
stringToParse = stringToParse.toSource().replace("\\x", "\x");
stringToParse = eval(stringToParse);
console.log(stringToParse);
mainly i needed this to parse mixed strings... as in string literals with hex mixed in
Given a string, how can I match all e-mails thar are inside "< >".
For example:
I can have xxx#abc.com and <yyy#abc.com> and I only want to match the yyy#abc.com.
Thanks!
To be really thorough you could implement a regex from RFC822, which describes valid email address formats, however, you could save time and headache by doing something quick and simple like this:
var extractEmailAddress = function(s) {
var r=/<?(\S+#[^\s>]+)>?/, m=(""+s).match(r);
return (m) ? m[1] : undefined;
};
extractEmailAddress('xxx#abc.com'); // => "xxx#abc.com"
extractEmailAddress('<yyy#abc.com>'); // => "yyy#abc.com"
Of course, this function will be very permissive of strings that might conceivably even remotely look like an email address, so the regular expression "r" could be improved if quality is a concern.
I am trying to use an HTML form and javascript (i mention this, because some advanced features of regex processing are not available when using it on javascript) to acomplish the following:
feed the form some text, and use a regex to look into it and "capture" certain parts of it to be used as variables...
i.e. the text is:
"abcde email: asdf#gfds.com email: fake#mail.net sdfsdaf..."
... now, my problem is that I cannot think of an elegant way of capturing both emails as the variables e1 and e2, for example.
the regex I have so far is something like this: /email: (\b\w+\b)/g but for some reason, this is not giving back the 2 matches... it only gives back asdf#gfds.com ><
sugestions?
You can use RegExp.exec() to repeatedly apply a regex to a string, returning a new match each time:
var entry = "[...]"; //Whatever your data entry is
var regex = /email: (\b\w+\b)/g
var emails = []
while ((match = regex.exec(entry))) {
emails[emails.length] = match[1];
}
I stored all the e-mails in an array (so as to make this work far arbitrary input). It looks like your regex might be a little off, too; you'll have to change it if you just want to capture the full e-mail.