Split a given string and get last split value [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a string like
count-contribute-1
count-contribute-11
count-contribute-1111
Here I want to split the string and get the last split value (i.e 1 , 11, 1111);
How can I do it?

split() on - and pop() of the last value
string.split('-').pop()

Use .pop() to get the last item from the array created by .split()
"count-contribute-1".split('-').pop();

Also you can get last part of numbers using regular expression. Like this:
s = "count-contribute-111"
s.match(/\d+$/)
//return "111"
It doesn't matter what separator you use.
s = "count-contribute-+*%111"
s.match(/\d+$/)
//return "111"

Related

How to remove specific characters in a string that changes based on request? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 months ago.
Improve this question
I have a string that looks like "{`index`:`20`,`value`:`RA`}<1", and I want it to become "`RA`<1". I don't think the replace function is sufficient as the index and value changes based on what I enter. Is there a way to do that?
assuming the required string is everything after the last :, except for the }, the original string can be sliced and edited as follows:
const string = "{`index`:`20`,`value`:`RA`}<1";
let newString = string.slice(string.lastIndexOf(":")+1).split("}").join("");
console.log(newString);
This will always append everything after the final } to everthing before it but after the last :, regardless of the earlier content.

javascript remove leading zeros from a number [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
const a = 043
console.log(Number(a))
Here the variable a is octal because of which we get the result as 35.
Instead, I want the variable a to be a number 43.
Found results for removing leading zeros from a string(Remove leading zeros from a number in Javascript)
Because your value starts with 0, and its type is number instead of string, it will be recognized as octal, and when you use it directly, javascript will convert it to decimal
The fastest way you can use Number.prototype.toString(radix)
let a = 043
console.log(a.toString(8))

How to Convert ["abc","efg"] array to "["abc","efg"]" (string) using javascript? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I'm asked to convert ["abc","efg"]
output should be "["abc","efg"]"
How can I do it using javascript?
I have tried searching so much, but couldn't find anything.
Any help will be appreciated.
You can stringify the value using JSON.stringify():
The JSON.stringify() method converts a JavaScript object or value to a JSON string, optionally replacing values if a replacer function is specified or optionally including only the specified properties if a replacer array is specified.
var valArr = ["abc","efg"];
var valStr = JSON.stringify(valArr);
console.log(valStr);
console.log('The type of valStr is:', typeof(valStr))
console.log(JSON.stringify(["abc","efg"]))
Note this will look like "[\"abc\",\"efg\"]" when expressed as a double-quoted string since the inner quotes must be escaped. The value is ["abc","efg"] when printed, which is a string.

How to Remove two consecutive double quotes from array after split [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a requirement where i will get the input from the user. He can enter
"features1,feature2",feature3,"feature4"
something like this. I'm using this
let arr: any = message.split(/,(?=(?:(?:[^"]*"){2})*[^"]*$)/)
to split the string and i'm getting result like
[""feature1,feature2"","feature3",""feature4""]
but i want the result like this
["feature1,feature2","feature3","feature4"]
I don't thing what you're trying to achieve is possible only using split function.
After you split, loop over the error and replace " with `` (empty).
const message = `"features1,feature2",feature3,"feature4"`;
let arr = message.split(/,(?=(?:(?:[^"]*"){2})*[^"]*$)/);
arr = arr.map(el=>el.replace(/"/g,''));
console.log(arr);

Regular express to match `command--o1--o2--o3` [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a string like command--o1--o2--o3 ( command,o1,o2,o3 are arbitrary words)
And I want get [o1, o2, o3] though a Regular Expression(Not a array operation or other ways. JUST only use Regular Expression).
Is there any idea to accomplish this !?
If you're using JavaScript, and assuming you want all strings after a --, you may do
var things = str.split(/--/).slice(1)
If you just want to get the 2 characters words following --, then you may use
var things = str.match(/--\w\w/g).map(function(s){ return s.slice(2) })

Categories

Resources