In python, one can use “”” to wrap long MySQL statements. For example, However, if I try the same thing in javascript, there will be syntax error. Is there some kind of javascript equivalent for python’s “””string encapsulation? If no, what are some best practices for encapsulating a long MySQL string statement in javascript? I am using node.js restify client.
Tag: string
Replace every nth character from a string
I have this JavaScript: I aim to replace every fourth letter in the string abcdefoihewfojias with |, so it becomes abc|efo|….etc,but I do not have a clue how to do this. Answer To support re-usability and the option to wrap this in an object/function let’s parameterise it:
In JavaScript, what is the simplest way to insert text into a string?
I have an issue where I need to take a string (which is a query string) in JavaScript and transform it into another query string (to avoid clashes). The original string I have comes in as: And I want to take a prefix (in my case it will be something like “slides[0].” and put it in front of all of
Trim specific character from a string
What’s the JavaScript equivalent to this C# Method: C# trims the selected character only at the beginning and end of the string! Answer One line is enough: A general solution: Parameter c is expected to be a character (a string of length 1). As mentionned in the comments, it might be useful to support multiple characters, as it’s quite common
Use dynamic (variable) string as regex pattern in JavaScript
I want to add a (variable) tag to values with regex, the pattern works fine with PHP but I have troubles implementing it into JavaScript. The pattern is (value is the variable): I escaped the backslashes: But this seem not to be right, I logged the pattern and its exactly what it should be. Any ideas? Answer To create the
Replace multiple strings with multiple other strings
I’m trying to replace multiple words in a string with multiple other words. The string is “I have a cat, a dog, and a goat.” However, this does not produce “I have a dog, a goat, and a cat”, but instead it produces “I have a cat, a cat, and a cat”. Is it possible to replace multiple strings with
Isolate part of a string from index to next whitespace?
What’s the best way to do this solve for this next whitespace? result: things Answer You could do the whole thing with a regular expression: What that does is match an exclamation point, followed by some amount of non-whitespace (that’s what S means — s with a lower-case “s” matches whitespace, and upper-case “s” is the opposite). The non-whitespace match
Replace each leading and trailing whitespace with underscore using regex in javascript
The output should look like this This code works fine for the trailing whitespaces but all the leading whitespaces are replaced with just one underscore. The working php regex for this is: /Gs|s(?=s*$)/ Answer Not pretty, but gets the job done
Javascript string-replace function ignoring hyphen
I have the following code: The alert gives hello,,..there I would expect it to give hello—,,..there as all the characters, including hyphens, are specified as exceptions in the replacement function. What am I doing wrong? Answer Escape the hyphen:
Extract substring by utf-8 byte positions
I have a string and start and length with which to extract a substring. Both positions (start and length) are based on the byte offsets in the original UTF8 string. However, there is a problem: The start and length are in bytes, so I cannot use “substring”. The UTF8 string contains several multi-byte characters. Is there a hyper-efficient way of