How can I split data in javascript having | symbol? [duplicate] - javascript

This question already has answers here:
How can I remove a character from a string using JavaScript?
(22 answers)
Closed 5 years ago.
I have data like
var result = 'Zika NAA, Blood|Zika NAA, Urine'
My requirement is
var result = Zika NAA, Blood
Zika NAA, Urine
| symbol shouldn't be there. How to achieve in javascript ?

document.write('Zika NAA, Blood|Zika NAA, Urine'.split('|').join('<br>'));

var result = 'Zika NAA, Blood|Zika NAA, Urine'.replace('|', ' ');
console.log(result);

Related

javascript: how to save string substitution in another string? [duplicate]

This question already has answers here:
JavaScript equivalent to printf/String.Format
(59 answers)
Closed 1 year ago.
I have a string and array
var s1 = "hello %s, i am %d years old";
var s2 =[John,24];
Expected result:
s3 = hello John i am 24 years old
I want to save the output into another string.
I'm able to display output in console
console.log(s1, ...s2)
But not able to store in other string.
I tried many things like:
var s3 = s1.format(...s2)
Any suggestions?
Unfortunately there is no string formatter available in JS, you'd have to write that manually like
let i = 0;
s3 = s1.replace(/%(s|d)/g, (_, type) => s2[i++]);
You could use the template string from ES6:
let anotherString = `Hello ${s2[0]}, I am ${s2[1]} years old`;
You could use this too:
String.prototype.format = function() {
a = this;
for (k in arguments) {
a = a.replace("{" + k + "}", arguments[k])
}
return a
}
Usage:
let anotherString = '{0} {1}!'.format('Hello', 'Word');
Than those solutions I dont see any other way to do that.

Split a string into two using pure javascript [duplicate]

This question already has answers here:
How to use split?
(4 answers)
Closed 7 years ago.
I have a string like this 132+456 or 132-456 or 132*456..etc ,it changes dynamically but I need to split this into 132 and 456 how to do it using pure java script?
It should be as easy as:
var parts = '132+456'.split('+');
parts[0]; //132
parts[1]; //456
Like so
var data = '132+456'.split('+');
var a = data[0];
var b = data[1];
// document.writeln only for example
document.writeln(a);
document.writeln(b);
Java
String[] values='132+456'.split('+');
String firstPart=value[0]; // has 132
for js
var data = '132+456'.split('+');
var a = data[0];
var b = data[1];

Issue in URL Splitting [duplicate]

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 8 years ago.
I have write following code to split the URL
var href = document.URL;
var GlobalBoomGuid = href.split("=");
var optionid = GlobalBoomGuid[1];
If i have URL like this : www.mydomain.com?optionid=655 It's giving me "655" .
But in current scenario my URL is www.mydomain.com?optionid=655#&ui-state=dialog and it's returning me 655#&ui-state
Now i want only id . How should i do that ?
Please if you dont like the question don't mark as a Negative
Thanx in Advance :)
This gets your result
var href = document.URL;
var foo = href.split("=")[1];
var GlobalBoomGuid = foo.split("#")[0];
var optionid = GlobalBoomGuid;
fiddle

How to make a javaScript variable equal to something or something else? [duplicate]

This question already has answers here:
Get a random item from a JavaScript array [duplicate]
(13 answers)
Closed 8 years ago.
Trying to add randomness to which image is displayed. Issue with saying calling and or statement.
var image = url3 | url2 | url1;
buildImage(image);
function buildImage(imagesrc) {
var img = document.createElement('img');
img.src = imagesrc;
document.getElementById('content').appendChild(img);
var images = [url3, url2, url1];
var image = images[(Math.random() * images.length)|0];
The "or" operators are not probabilistic "or" that will return one value or another. They are very specific: || will return the first value unless it's falsy, in which case it returns the second; and | converts both arguments to integers and applies the same operation to each individual binary digit.
Instead of using separate variables, make a urls array:
var urls = [
"http://example.com/img1.jpg",
"http://example.com/img2.jpg",
"http://example.com/img3.jpg"
];
You can then select a random URL by doing:
var url = urls[Math.floor(Math.random() * urls.length)];
And call buildImage:
buildImage(url);

How can I read a GET element from url via javascript? [duplicate]

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 9 years ago.
My URL looks like this one:
http://www.something.hu/#/ask?kerdesPost=fdasdas%20ad%20asd%20ad%20asdas
I would like to get only fdasdas%20ad%20asd%20ad%20asdas or only kerdesPost=fdasdas%20ad%20asd%20ad%20asdas.
How can I do it via JavaScript? What is the shortest way?
You can try the following:
if((window.location.href).indexOf('?') != -1) {
var queryString = (window.location.href).substr((window.location.href).indexOf('?') + 1);
// "queryString" will now contain kerdesPost=fdasdas%20ad%20asd%20ad%20asdas
var value = (queryString.split('='))[1];
// "value" will now contain fdasdas%20ad%20asd%20ad%20asdas
value = decodeURIComponent(value);
// "value" will now contain fdasdas ad asd ad asdas (unescaped value)
}
JSFiddle
This should get you what you need.

Categories