I'm currently learning NodeJS after working with Python for the last few years.
In Python I was able to save a string inside a JSON with dynamic parameters and set them once the string loaded, for example:
MY JSON:
j = {
"dynamicText": "Hello %s, how are you?"
}
and then use my string like that:
print(j['dynamicText'] % ("Dan"))
so Python replaces the %s with "Dan".
I am looking for the JS equivalent but could not find one. Any ideas?
** Forgot to mention: I want to save the JSON as another config file so literals won't work here
Use template literal. This is comparatively new and may not support ancient browsers
var test = "Dan"
var j = {
"dynamicText": `Hello ${test}, how are you?`
}
console.log(j["dynamicText"])
Alternatively you can create a function and inside that function use string.replace method to to replace a word with new word
var test = "Dan"
var j = {
"dynamicText": "Hello %s, how are you?"
}
function replace(toReplaceText, replaceWith) {
let objText = j["dynamicText"].replace(toReplaceText, replaceWith);
return objText;
}
console.log(replace('%s', test))
There is no predefined way in JavaScript, but you could still achieve something like below. Which I have done in my existing Application.
function formatString(str, ...params) {
for (let i = 0; i < params.length; i++) {
var reg = new RegExp("\\{" + i + "\\}", "gm");
str = str.replace(reg, params[i]);
}
return str;
}
now formatString('You have {0} cars and {1} bikes', 'two', 'three') returns 'You have two cars and three bikes'
In this way if {0} repeats in String it replaces all.
like formatString('You have {0} cars, {1} bikes and {0} jeeps', 'two', 'three') to "You have two cars, three bikes and two jeeps"
Hope this helps.
You can write a String.format method, using regex, and the String.replace method:
String.prototype.format = function() {
var args = arguments;
return this.replace(/{(\d+)}/g, (match, p1) => {
var i = parseInt(p1);
return typeof args[i] != 'undefined' ? args[i] : match;
});
}
After that, running:
console.log("{0}{1}".format("John", "Doe"));
Will output: John Doe
Of course, if you don't like editing the prototype of objects you don't own (it is generally good practice), you can just create a function:
var format = function(str) {
var args = arguments;
return str.replace(/{(\d+)}/g, (match, p1) => {
var i = parseInt(p1);
return typeof args[i+1] != 'undefined' ? args[i+1] : match;
});
}
For a few nice alternatives, you may want to take a look at JavaScript equivalent to printf/string.format
While it's asking for a C-like printf() equivalent for JS, the answers would also apply to your question, since Python format strings are inspired by C.
I've found a great npm module that does exactly what I was needed - string-format.
String-Format in NPM
Related
disclaimer - absolutely new to regexes....
I have a string like this:
subject=something||x-access-token=something
For this I need to extract two values. Subject and x-access-token.
As a starting point, I wanted to collect two strings: subject= and x-access-token=. For this here is what I did:
/[a-z,-]+=/g.exec(mystring)
It returns only one element subject=. I expected both of them. Where i am doing wrong?
The g modifier does not affect exec, because exec only returns the first match by specification. What you want is the match method:
mystring.match(/[a-z,-]+=/g)
No regex necessary. Write a tiny parser, it's easy.
function parseValues(str) {
var result = {};
str.split("||").forEach(function (item) {
var parts = item.split("=");
result[ parts[0] /* key */ ] = parts[1]; /* value */
});
return result;
}
usage
var obj = parseValues("subject=something||x-access-token=something-else");
// -> {subject: "something", x-access-token: "something-else"}
var subj = obj.subject;
// -> "something"
var token = obj["x-access-token"];
// -> "something-else"
Additional complications my arise when there is an escaping schema involved that allows you to have || inside a value, or when a value can contain an =.
You will hit these complications with regex approach as well, but with a parser-based approach they will be much easier to solve.
You have to execute exec twice to get 2 extracted strings.
According to MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec
If your regular expression uses the "g" flag, you can use the exec() method multiple times to find successive matches in the same string.
Usually, people extract all strings matching the pattern one by one with a while loop. Please execute following code in browser console to see how it works.
var regex = /[a-z,-]+=/g;
var string = "subject=something||x-access-token=something";
while(matched = regex.exec(string)) console.log(matched);
You can convert the string into a valid JSON string, then parse it to retrieve an object containing the expected data.
var str = 'subject=something||x-access-token=something';
var obj = JSON.parse('{"' + str.replace(/=/g, '":"').replace(/\|\|/g, '","') + '"}');
console.log(obj);
I don't think you need regexp here, just use the javascript builtin function "split".
var s = "subject=something1||x-access-token=something2";
var r = s.split('||'); // r now is an array: ["subject=something1", "x-access-token=something2"]
var i;
for(i=0; i<r.length; i++){
// for each array's item, split again
r[i] = r[i].split('=');
}
At the end you have a matrix like the following:
y x 0 1
0 subject something1
1 x-access-token something2
And you can access the elements using x and y:
"subject" == r[0][0]
"x-access-token" == r[1][0]
"something2" == r[1][1]
If you really want to do it with a pure regexp:
var input = 'subject=something1||x-access-token=something2'
var m = /subject=(.*)\|\|x-access-token=(.*)/.exec(input)
var subject = m[1]
var xAccessToken = m[2]
console.log(subject);
console.log(xAccessToken);
However, it would probably be cleaner to split it instead:
console.log('subject=something||x-access-token=something'
.split(/\|\|/)
.map(function(a) {
a = a.split(/=/);
return { key: a[0], val: a[1] }
}));
I have an string, but at certain points in the string it has dynamic values.
I want to find those points in the string and replace them with values from an array.
Let's say I have:
var array = ['string', 'value'];
var string = 'This is a {1}, and this is a {2}.';
Now I need to figure out a way to replace {1}, with the value of the first index in the array and replace {2} with the value of the second index of the array.
So the string would like this:
This is a string, and this is a value.
This seems so simple, but I am breaking my head on this. I can't find a way to do this easily.
You can use replace with a regular expression and a function:
string.replace(/{(\d+)}/g, function(match, n) {
return array[n-1];
});
You can also check n < 1 || n > array.length to provide a fallback value in case the number is out of bounds.
Alternatively, the new ES6 way is using tagged template strings
function tag(strings, ...values) {
var parts = [];
for(var i=0; i<strings.length; ++i)
parts.push(strings[i], array[values[i]-1]);
return parts.join('');
}
tag`This is a ${1}, and this is a ${2}.`
like this
string.replace(/\{(\d+)\}/g, function(m, n){
return array[n-1]
})
You can use Array.prototype.reduce:
var StringHelper = {
format: function(format, args) {
return args.reduce(function(result, currentReplace, currentReplaceIndex) {
result = result.replace("{" + (currentReplaceIndex + 1) + "}", args[currentReplaceIndex]);
return result;
}, format);
}
};
var sourceString = "My name is {1} and I'm {2} years old";
var replaceItems = ["Matías", "31"];
var parsedString = StringHelper.format(sourceString, replaceItems);
alert(parsedString);
It's a good alternative to regular expressions, and I'm not absolutely sure if I did a jsPerf test in the right way, but it shows that this approach outperforms the regular expression one.
I have a string that and I am trying to extract the characters before the quote.
Example is extract the 14 from 14' - €14.99
I am using the follwing code to acheive this.
$menuItem.text().match(/[^']*/)[0]
My problem is that if the string is something like €0.88 I wish to get an empty string returned. However I get back the full string of €0.88.
What I am I doing wrong with the match?
This is the what you should use to split:
string.slice(0, string.indexOf("'"));
And then to handle your non existant value edge case:
function split(str) {
var i = str.indexOf("'");
if(i > 0)
return str.slice(0, i);
else
return "";
}
Demo on JsFiddle
Nobody seems to have presented what seems to me as the safest and most obvious option that covers each of the cases the OP asked about so I thought I'd offer this:
function getCharsBefore(str, chr) {
var index = str.indexOf(chr);
if (index != -1) {
return(str.substring(0, index));
}
return("");
}
try this
str.substring(0,str.indexOf("'"));
Here is an underscore mixin in coffescript
_.mixin
substrBefore : ->
[char, str] = arguments
return "" unless char?
fn = (s)-> s.substr(0,s.indexOf(char)+1)
return fn(str) if str?
fn
or if you prefer raw javascript : http://jsfiddle.net/snrobot/XsuQd/
You can use this to build a partial like:
var beforeQuote = _.substrBefore("'");
var hasQuote = beforeQuote("14' - €0.88"); // hasQuote = "14'"
var noQoute = beforeQuote("14 €0.88"); // noQuote = ""
Or just call it directly with your string
var beforeQuote = _.substrBefore("'", "14' - €0.88"); // beforeQuote = "14'"
I purposely chose to leave the search character in the results to match its complement mixin substrAfter (here is a demo: http://jsfiddle.net/snrobot/SEAZr/ ). The later mixin was written as a utility to parse url queries. In some cases I am just using location.search which returns a string with the leading ?.
I use "split":
let string = "one-two-three";
let um = string.split('-')[0];
let dois = string.split('-')[1];
let tres = string.split('-')[2];
document.write(tres) //three
I have a string with say: My Name is %NAME% and my age is %AGE%.
%XXX% are placeholders. We need to substitute values there from an object.
Object looks like: {"%NAME%":"Mike","%AGE%":"26","%EVENT%":"20"}
I need to parse the object and replace the string with corresponding values. So that final output will be:
My Name is Mike and my age is 26.
The whole thing has to be done either using pure javascript or jquery.
The requirements of the original question clearly couldn't benefit from string interpolation, as it seems like it's a runtime processing of arbitrary replacement keys.
However, if you just had to do string interpolation, you can use:
const str = `My name is ${replacements.name} and my age is ${replacements.age}.`
Note the backticks delimiting the string, they are required.
For an answer suiting the particular OP's requirement, you could use String.prototype.replace() for the replacements.
The following code will handle all matches and not touch ones without a replacement (so long as your replacement values are all strings, if not, see below).
var replacements = {"%NAME%":"Mike","%AGE%":"26","%EVENT%":"20"},
str = 'My Name is %NAME% and my age is %AGE%.';
str = str.replace(/%\w+%/g, function(all) {
return replacements[all] || all;
});
jsFiddle.
If some of your replacements are not strings, be sure they exists in the object first. If you have a format like the example, i.e. wrapped in percentage signs, you can use the in operator to achieve this.
jsFiddle.
However, if your format doesn't have a special format, i.e. any string, and your replacements object doesn't have a null prototype, use Object.prototype.hasOwnProperty(), unless you can guarantee that none of your potential replaced substrings will clash with property names on the prototype.
jsFiddle.
Otherwise, if your replacement string was 'hasOwnProperty', you would get a resultant messed up string.
jsFiddle.
As a side note, you should be called replacements an Object, not an Array.
How about using ES6 template literals?
var a = "cat";
var b = "fat";
console.log(`my ${a} is ${b}`); //notice back-ticked string
More about template literals...
Currently there is still no native solution in Javascript for this behavior. Tagged templates are something related, but don't solve it.
Here there is a refactor of alex's solution with an object for replacements.
The solution uses arrow functions and a similar syntax for the placeholders as the native Javascript interpolation in template literals ({} instead of %%). Also there is no need to include delimiters (%) in the names of the replacements.
There are two flavors (three with the update): descriptive, reduced, elegant reduced with groups.
Descriptive solution:
const stringWithPlaceholders = 'My Name is {name} and my age is {age}.';
const replacements = {
name: 'Mike',
age: '26',
};
const string = stringWithPlaceholders.replace(
/{\w+}/g,
placeholderWithDelimiters => {
const placeholderWithoutDelimiters = placeholderWithDelimiters.substring(
1,
placeholderWithDelimiters.length - 1,
);
const stringReplacement = replacements[placeholderWithoutDelimiters] || placeholderWithDelimiters;
return stringReplacement;
},
);
console.log(string);
Reduced solution:
const stringWithPlaceholders = 'My Name is {name} and my age is {age}.';
const replacements = {
name: 'Mike',
age: '26',
};
const string = stringWithPlaceholders.replace(/{\w+}/g, placeholder =>
replacements[placeholder.substring(1, placeholder.length - 1)] || placeholder
);
console.log(string);
UPDATE 2020-12-10
Elegant reduced solution with groups, as suggested by #Kade in the comments:
const stringWithPlaceholders = 'My Name is {name} and my age is {age}.';
const replacements = {
name: 'Mike',
age: '26',
};
const string = stringWithPlaceholders.replace(
/{(\w+)}/g,
(placeholderWithDelimiters, placeholderWithoutDelimiters) =>
replacements[placeholderWithoutDelimiters] || placeholderWithDelimiters
);
console.log(string);
UPDATE 2021-01-21
Support empty string as a replacement, as suggested by #Jesper in the comments:
const stringWithPlaceholders = 'My Name is {name} and my age is {age}.';
const replacements = {
name: 'Mike',
age: '',
};
const string = stringWithPlaceholders.replace(
/{(\w+)}/g,
(placeholderWithDelimiters, placeholderWithoutDelimiters) =>
replacements.hasOwnProperty(placeholderWithoutDelimiters) ?
replacements[placeholderWithoutDelimiters] : placeholderWithDelimiters
);
console.log(string);
You can use JQuery(jquery.validate.js) to make it work easily.
$.validator.format("My name is {0}, I'm {1} years old",["Bob","23"]);
Or if you want to use just that feature you can define that function and just use it like
function format(source, params) {
$.each(params,function (i, n) {
source = source.replace(new RegExp("\\{" + i + "\\}", "g"), n);
})
return source;
}
alert(format("{0} is a {1}", ["Michael", "Guy"]));
credit to jquery.validate.js team
As with modern browser, placeholder is supported by new version of Chrome / Firefox, similar as the C style function printf().
Placeholders:
%s String.
%d,%i Integer number.
%f Floating point number.
%o Object hyperlink.
e.g.
console.log("generation 0:\t%f, %f, %f", a1a1, a1a2, a2a2);
BTW, to see the output:
In Chrome, use shortcut Ctrl + Shift + J or F12 to open developer tool.
In Firefox, use shortcut Ctrl + Shift + K or F12 to open developer tool.
#Update - nodejs support
Seems nodejs don't support %f, instead, could use %d in nodejs.
With %d number will be printed as floating number, not just integer.
Just use replace()
var values = {"%NAME%":"Mike","%AGE%":"26","%EVENT%":"20"};
var substitutedString = "My Name is %NAME% and my age is %AGE%.".replace("%NAME%", $values["%NAME%"]).replace("%AGE%", $values["%AGE%"]);
You can use a custom replace function like this:
var str = "My Name is %NAME% and my age is %AGE%.";
var replaceData = {"%NAME%":"Mike","%AGE%":"26","%EVENT%":"20"};
function substitute(str, data) {
var output = str.replace(/%[^%]+%/g, function(match) {
if (match in data) {
return(data[match]);
} else {
return("");
}
});
return(output);
}
var output = substitute(str, replaceData);
You can see it work here: http://jsfiddle.net/jfriend00/DyCwk/.
If you want to do something closer to console.log like replacing %s placeholders like in
>console.log("Hello %s how are you %s is everything %s?", "Loreto", "today", "allright")
>Hello Loreto how are you today is everything allright?
I wrote this
function log() {
var args = Array.prototype.slice.call(arguments);
var rep= args.slice(1, args.length);
var i=0;
var output = args[0].replace(/%s/g, function(match,idx) {
var subst=rep.slice(i, ++i);
return( subst );
});
return(output);
}
res=log("Hello %s how are you %s is everything %s?", "Loreto", "today", "allright");
document.getElementById("console").innerHTML=res;
<span id="console"/>
you will get
>log("Hello %s how are you %s is everything %s?", "Loreto", "today", "allright")
>"Hello Loreto how are you today is everything allright?"
UPDATE
I have added a simple variant as String.prototype useful when dealing with string transformations, here is it:
String.prototype.log = function() {
var args = Array.prototype.slice.call(arguments);
var rep= args.slice(0, args.length);
var i=0;
var output = this.replace(/%s|%d|%f|%#/g, function(match,idx) {
var subst=rep.slice(i, ++i);
return( subst );
});
return output;
}
In that case you will do
"Hello %s how are you %s is everything %s?".log("Loreto", "today", "allright")
"Hello Loreto how are you today is everything allright?"
Try this version here
This allows you to do exactly that
NPM: https://www.npmjs.com/package/stringinject
GitHub: https://github.com/tjcafferkey/stringinject
By doing the following:
var str = stringInject("My username is {username} on {platform}", { username: "tjcafferkey", platform: "GitHub" });
// My username is tjcafferkey on Git
I have written a code that lets you format string easily.
Use this function.
function format() {
if (arguments.length === 0) {
throw "No arguments";
}
const string = arguments[0];
const lst = string.split("{}");
if (lst.length !== arguments.length) {
throw "Placeholder format mismatched";
}
let string2 = "";
let off = 1;
for (let i = 0; i < lst.length; i++) {
if (off < arguments.length) {
string2 += lst[i] + arguments[off++]
} else {
string2 += lst[i]
}
}
return string2;
}
Example
format('My Name is {} and my age is {}', 'Mike', 26);
Output
My Name is Mike and my age is 26
Another solution if you're using node.js is StackExchange's own formatUnicorn utility function (https://www.npmjs.com/package/format-unicorn):
let x = {name:'jason', food:'pine cones'};
let s = '{name} enjoys a delicious bowl of {food}';
let formatted = x.formatUnicorn(s);
Also, a bit of an edge case, but if you aren't using Node but you do just happen to be writing a userscript for SE sites, then formatUnicorn will already be on the String prototype.
As a quick example:
var name = 'jack';
var age = 40;
console.log('%s is %d yrs old',name,age);
The output is:
jack is 40 yrs old
Here is another way of doing this by using es6 template literals dynamically at runtime.
const str = 'My name is ${name} and my age is ${age}.'
const obj = {name:'Simon', age:'33'}
const result = new Function('const {' + Object.keys(obj).join(',') + '} = this.obj;return `' + str + '`').call({obj})
document.body.innerHTML = result
const stringInject = (str = '', obj = {}) => {
let newStr = str;
Object.keys(obj).forEach((key) => {
let placeHolder = `#${key}#`;
if(newStr.includes(placeHolder)) {
newStr = newStr.replace(placeHolder, obj[key] || " ");
}
});
return newStr;
}
Input: stringInject("Hi #name#, How are you?", {name: "Ram"});
Output: "Hi Ram, How are you?"
ES6:
const strFormat = (str, ...args) => args.reduce((s, v) => s.replace('%s', v), str);
// Use it like:
const result = strFormat('%s is %s yrs old', 'name', 23);
Lots of good/similar answers here. I wanted the ability to easily get a nested key in an object (or perhaps some JSON data structure) for substitution, so I took the following simple approach:
const getKey = (d, path) => {
// path can be a string like 'key1.key2' or an iterable of keys
if (typeof(path) === 'string') {
path = path.split('.')
}
return path.reduce((x, y) => x[y], d)
}
const inject = (str, obj) => str.replace(/\${(.*?)}/g, (x,g)=> getKey(obj, g));
// Example
> const str = 'there are ${a} ways to ${b.c}'
undefined
> inject(str, {'a':'many', 'b': {'c': 'skin a cat'}})
'there are many ways to skin a cat'
Some inspiration from this and this.
This is a merged solution of Gerson Diniz and Shubham Vyas.
It is possible to pass a set of arguments or an object.
function strSwap(str) {
if (!str) return null;
let args = [];
for (let a of arguments)
args.push(a);
args.shift();
if (!args.length) return null;
// replacement by object - {{prop}}
if (!!(args[0].constructor && args[0].constructor.name.toLowerCase() === 'object')) {
for (let i in args[0]) {
let n = `{{${i}}}`;
str = str.includes(n) ? str.replaceAll(n, args[0][i] + '') : str;
}
}
// replacement by placeholders - %s
else {
str = args.reduce((s, v) => s.replace('%s', v), str);
}
return str;
}
// ---------------------
console.log(strSwap('Hello %s, my name is %s.', 'alice', 'bob'));
console.log(strSwap('Hello {{a}}, my name is {{b}}. Hello {{b}}.', {
a: 'alice',
b: 'bob'
}));
I'm trying to reverse an input string
var oneway = document.getElementById('input_field').value();
var backway = oneway.reverse();
but firebug is telling me that oneway.reverse() is not a function. Any ideas?
Thank you
reverse() is a method of array instances. It won't directly work on a string. You should first split the characters of the string into an array, reverse the array and then join back into a string:
var backway = oneway.split("").reverse().join("");
Update
The method above is only safe for "regular" strings. Please see comment by Mathias Bynens below and also his answer for a safe reverse method.
The following technique (or similar) is commonly used to reverse a string in JavaScript:
// Don’t use this!
var naiveReverse = function(string) {
return string.split('').reverse().join('');
}
In fact, all the answers posted so far are a variation of this pattern. However, there are some problems with this solution. For example:
naiveReverse('foo 𝌆 bar');
// → 'rab �� oof'
// Where did the `𝌆` symbol go? Whoops!
If you’re wondering why this happens, read up on JavaScript’s internal character encoding. (TL;DR: 𝌆 is an astral symbol, and JavaScript exposes it as two separate code units.)
But there’s more:
// To see which symbols are being used here, check:
// http://mothereff.in/js-escapes#1ma%C3%B1ana%20man%CC%83ana
naiveReverse('mañana mañana');
// → 'anãnam anañam'
// Wait, so now the tilde is applied to the `a` instead of the `n`? WAT.
A good string to test string reverse implementations is the following:
'foo 𝌆 bar mañana mañana'
Why? Because it contains an astral symbol (𝌆) (which are represented by surrogate pairs in JavaScript) and a combining mark (the ñ in the last mañana actually consists of two symbols: U+006E LATIN SMALL LETTER N and U+0303 COMBINING TILDE).
The order in which surrogate pairs appear cannot be reversed, else the astral symbol won’t show up anymore in the ‘reversed’ string. That’s why you saw those �� marks in the output for the previous example.
Combining marks always get applied to the previous symbol, so you have to treat both the main symbol (U+006E LATIN SMALL LETTER N) as the combining mark (U+0303 COMBINING TILDE) as a whole. Reversing their order will cause the combining mark to be paired with another symbol in the string. That’s why the example output had ã instead of ñ.
Hopefully, this explains why all the answers posted so far are wrong.
To answer your initial question — how to [properly] reverse a string in JavaScript —, I’ve written a small JavaScript library that is capable of Unicode-aware string reversal. It doesn’t have any of the issues I just mentioned. The library is called Esrever; its code is on GitHub, and it works in pretty much any JavaScript environment. It comes with a shell utility/binary, so you can easily reverse strings from your terminal if you want.
var input = 'foo 𝌆 bar mañana mañana';
esrever.reverse(input);
// → 'anañam anañam rab 𝌆 oof'
String.prototype.reverse = function () {
return this.split("").reverse().join("");
}
Inspired by the first result I got when I did a Google for javascript string reverse.
Google harder, bros. This is by Edd Mann.
function reverse (s) {
for (var i = s.length - 1, o = ''; i >= 0; o += s[i--]) { }
return o;
}
http://eddmann.com/posts/ten-ways-to-reverse-a-string-in-javascript/
http://jsperf.com/string-reverse-function-performance
reverse is a function on an array and that is a string. You could explode the string into an array and then reverse it and then combine it back together though.
var str = '0123456789';
var rev_str = str.split('').reverse().join('');
// You could reverse a string without creating an array
String.prototype.reverse= function(){
var s= '', L= this.length;
while(L){
s+= this[--L];
}
return s;
}
var s1= 'the time has come, the walrus said, to speak of many things';
s1.reverse()
/*returned value: (String)
sgniht ynam fo kaeps ot, dias surlaw eht, emoc sah emit eht
*/
This is probably the way, acceptable for all browsers:
function reverse(s) {
var o = '';
for (var i = s.length - 1; i >= 0; i--)
o += s[i];
return o;
}
Call it like a charm:
reverse('your_string');
I like to share some notable implementations for string reverse.
split,reverse,join
const reverseString = (str) => str.split('') .reverse() .join('');
reduce
const reverseString =(str) => [...str].reduce((acc, cur) => cur + acc);
append last one by one
const reverseString = (str) => {
const ary = [];
for(let char of str) {
ary.unshift(char);
}
return ary.join('');
}
recursion
const reverseString =(str)=> (str === '') ? '' : reverseString(str.substr(1)) + str[0];
two pointer approach
const reverseString = (str) => {
const strArr = Array.from(str);
let start = 0;
let end = str.length - 1;
while (start <= end) {
const temp = strArr[start];
strArr[start] = strArr[end];
strArr[end] = temp;
start++;
end--;
}
return strArr.join("");
}
//Using reverse with split, reverse , join
function reverseString1(str) {
return str
.split('') // alternative [...str], Array.from(str)
.reverse() // alternative .sort(() => -1)
.join('');
}
// Using reduce
function reverseString2(str) {
return [...str].reduce((accumulator, current) => current + accumulator) // reduce from left to right
//alternative [...str].reduceRight((accumulator, current) => accumulator + current); // reduce right to left
}
// append to last one by one
function reverseString3(str){
const ary = [];
for(let char of str) {
ary.unshift(char);
}
return ary.join('');
}
// recursion with ternary with substr
function reverseString4(str) {
return (str === '') ? '' : reverseString4(str.substr(1)) + str[0];
}
// two pointer approach [less time complexity O(n)]
// front back chars exchanging
function reverseString5(str) {
const strArr = Array.from(str); // alternative [...str],str.split('')
let start = 0;
let end = str.length - 1;
while (start <= end) {
const temp = strArr[start];
strArr[start] = strArr[end];
strArr[end] = temp;
start++;
end--;
}
return strArr.join("");
}
console.log(reverseString1("Hello World"))
console.log(reverseString2("Hello World"))
console.log(reverseString3("Hello World"))
console.log(reverseString4("Hello World"))
console.log(reverseString5("Hello World"))
//=> dlroW olleH
Note:
Built-in method works well for ASCII inputs, not unicode things.. so use spread operation inspite of split. Check out split vs spread implementation
Extra:
In-Place string reverse is not possible in JS. Check out in-place reverse
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function () {
$("#stringInput").keyup(function () {
debugger;
var string = $("#stringInput").val();
var stringArray = [];
sInput = string.toString();
for (var i = 0, len = sInput.length; i < len; i++) {
stringArray.push(sInput.charAt(i));
}
stringArray.reverse();
str = stringArray.join('');
document.getElementById("stringOutput").value = str;
});
$("button").click(function () {
debugger;
$("#myForm").toggle();
});
});
</script>
</head>
<body>
<div>
<form name="myForm" id="myForm">
<table>
<tr>
<td>Insert Elements :</td>
<td><input type="text" name="stringInput" id="stringInput"/></td>
<td>Output :</td>
<td><input type="text" id="stringOutput" name="stringOutput" readonly="true" /></td>
</tr>
</table>
</form>
<button>Show/Hide</button>
</div>
</body>
</html>
Reverse String using function parameter with error handling :
function reverseString(s)
{
try
{
console.log(s.split("").reverse().join(""));
}
catch(e)
{
console.log(e.message);
console.log(s);
}
}
I think you'll find that in fact reverse() isn't a function in jQuery. Incidentally, jQuery is really good at manipulating your DOM, but isn't really for string manipulation as such (although you can probably get plugins/write your own) to do this.
The best way I've found to reverse a string in javascript is to do the following:
String.prototype.reverse = function(){
splitext = this.split("");
revertext = splitext.reverse();
reversed = revertext.join("");
return reversed;
}
Found at: http://www.bytemycode.com/snippets/snippet/400/
I think you'll find that if you pop the above into your code somewhere, your call to .reverse() should work :)
Mathias Bynens, your code works grate, thanks a lot!
I convert your code to a function, in this way users are able to copy it from here.
Thanks!
//The function reverse a string, JavaScript’s has internal character encoding so we are
//unable to reverse the string in the "easy ways". For example the TL;DR: 𝌆 is an astral
//symbol, and JavaScript exposes it as two separate code units.
function ReverseString(string){
//-
var regexSymbolWithCombiningMarks = /([\0-\u02FF\u0370-\u1DBF\u1E00-\u20CF\u2100-\uD7FF\uDC00-\uFE1F\uFE30-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]|[\uD800-\uDBFF])([\u0300-\u036F\u1DC0-\u1DFF\u20D0-\u20FF\uFE20-\uFE2F]+)/g;
var regexSurrogatePair = /([\uD800-\uDBFF])([\uDC00-\uDFFF])/g;
//-
//Step 1: deal with combining marks and astral symbols (surrogate pairs)
string = string
//Swap symbols with their combining marks so the combining marks go first
.replace(regexSymbolWithCombiningMarks, function($0, $1, $2) {
return reverse($2) + $1;
})
// Swap high and low surrogates so the low surrogates go first
.replace(regexSurrogatePair, '$2$1');
// Step 2: reverse the code units in the string
var result = '';
var index = string.length;
while (index--) {
result += string.charAt(index);
}
//Return value
return result;
}
This reverse prototype function is implemented using "this".
If you see log console of "this", it will generate the array, and it has length property.
So that it!!! Just use reverse "for-loop" as shown in the code snippet.
String.prototype.reverse = function () {
console.log(this);
var result = "";
var len = this.length;
for (i = (len-1); i >= 0 ; i--) {
result += this[i];
}
return result;
};
alert("elahnu jaknap".reverse());
Use this simple method to reverse the words of a string at its position
function fun(str){
var arr1=str.split(' ');
console.log(arr1);
var strings='',rever='';
for(var i=0;i<arr1.length;i++)
{
strings=arr1[i].split('');
rever+=strings.reverse().join('')+' ';
}
console.log(rever.split(' '));
console.log(rever.trim());
};
fun('javascript is fun');
If it's necessary to revert the string, but return the original value of the error:
function reverseString(s) {
let valuePrintS;
try {
valuePrintS = s.split("").reverse().join("");
} catch {
console.log("s.split is not a function");
valuePrintS = s;
} finally {
console.log(valuePrintS);
}
}
I believe most performant solution with reduce like in https://stackoverflow.com/a/68978553/5018572 post
function reverse(str) {
return str.split("").reduce((final, letter) => letter + final);
}
console.log(reverse("Armaggedon"));
This is how I always do;
var rev = Array.prototype.reduce.call("Hello World",(p,c) => c+p);
console.log(rev);
Some people do it like [].prototype.redu... but that would be creating an array for no reason. Instead use the Array constructor.
Also some people ask why Array.prototype.reverse.call("Hello World") wouldn't work. It's so because .reverse() attempts to reverse the subject array in place and with immutable values in JS such as a String you can not do that.
String.prototype.strReverse = function() {
var newstring = "";
for (var s=0; s < this.length; s++) {
newstring = this.charAt(s) + newstring;
}
return newstring;
};