how to get second value from string in JavaScript? [duplicate] - javascript

This question already has answers here:
How do I split a string, breaking at a particular character?
(17 answers)
Closed 4 months ago.
I have a string with comma now i want to get first value how can i get it?
My Code:-
var data = "0.46609362959861755, 0.25069287419319153, 0.5107838958501816, 0.26014574989676476";
console.log(data.replaceAll(',','').splice(0,2));
Thanks for your efforts!

Maybe you need this?
data.split(',')[0]

console.log(data.split(', ')[0])
Use this instead of what you did.
data.split(', ')
returns an array with every element in your string, separately, with no spaces or commas attached. The [0] after it grabs the first element of the array.

Related

How to print first sentence from semicolon separated string? [duplicate]

This question already has answers here:
Getting text before a character in a string in Javascript
(4 answers)
Closed 3 months ago.
I have a column in database table that saves type string in it. It saves multiple lines separated by semicolon, something like this -
this is first sentence;this is second sentence;this is third sentence
I want to print first line only, i.e. -
this is first sentence
Any idea how can I achieve this using JavaScript?
Use String.prototype.split
const string ='this is first sentence;this is second sentence;this is third sentence'
const sentences = string.split(';')
const first = sentences[0]
console.log(first)

Replace String with value `$&' not working in javascript [duplicate]

This question already has answers here:
Why 'ABC'.replace('B', '$`') gives AAC
(2 answers)
Closed 1 year ago.
I have string show {{value}} and I want replace {{value}} with $& but it not work. It return current value show {{value}}.
Here is my code
let data ="show {{value}}";
let output = data.replace("{{value}}","$&");
alert(output);
I don't know why it not work. I try replace with other strings same $1, $a and it work.
How I can fix my problem
$ is a special symbol in javascript. Write $$& instead and it should work :)

Regex how to get what comes after a certain word in a string? [duplicate]

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 2 years ago.
http://localhost:3000/?code=85b1a3645tgreg1f54221d8d9f54923b88ade29945yttrtgdg903
I am trying to get whatever comes after 'code=' from this string. How could I do this?
You could use
code=(.+)
See a demo on regex101.com.
In JavaScript this could be:
let string = 'http://localhost:3000/?code=85b1a3645tgreg1f54221d8d9f54923b88ade29945yttrtgdg903';
let m = string.match(/code=(.+)/);
console.log(m[1]);
But it would possibly be more straight-forward to parse the url as it is and use the query part accordingly.
str.match(/code=(.*)/)[1] will do that for you.

Javascript regex that escapes \ sign [duplicate]

This question already has answers here:
Remove all backslashes in Javascript
(4 answers)
Closed 4 years ago.
I am trying to convert string that has following values
"A\"s\"sets"
my goal is to remove from string \ values no matter how many of them appear in string.
"A"s"sets"
I tried using new RegExp but I do not manage to perform that operation.
I even managed to create regex that will pick up everything except \ sign
[a-zA-Z0-9'"*]
I also tried calling on
regex.exec(string)
but I am getting an array instead of cleared string.
Anyone have any idea how to do this ?
Thank you
You can use replace.
let str = `"A\"s\\"sets"`
let op = str.replace(/\\+/g, '')
console.log(op)

Complicated JavaScript string removal/replacement [duplicate]

This question already has answers here:
How can I delete a query string parameter in JavaScript?
(27 answers)
Closed 7 years ago.
I have a query string that I need to remove a certain parameter from. For instance, my query string may be "?name=John&page=12&mfgid=320", and I need to remove the "page" parameter from it and end up with "?name=John&mfgid=320". I cannot assume that the "page" parameter is or isn't followed by other parameters.
All my attempts at using JavaScript functions/regex are failing miserably, so I could really use a hand in getting this working. Thanks.
That's quite easy... It's just /page=\d+&?/
var uri = '?name=John&page=12&mfgid=320';
uri = uri.replace(/page=\d+&?/,'');
You can use:
uri = uri.replace(/[?&]page=[^&\n]+$|([&?])page=[^&\n]+&/g, '$1');
RegEx Demo
We'll need to use alternation to cover all the cases of presence of query parameter. Check my demo for all test cases.

Categories

Resources