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 last year.
Improve this question
I have a need to split a string in javascript to individual components. Let me go through the example of the requirement I'm having.
This is the string I'm having:
"[InsertDelta, position: 63, lines: [return {]]"
I need to split the above string into three components as below:
InsertDela
position: 63
lines: [return {]
Is there any way to separate the strings like this in javascript?
You can use split to split the string at "," and then use for each to iterate through the items.
let y = "[InsertDelta, position: 63, lines: [return {]]";
let x = y.split(",");
x.forEach(item => console.log(item.replace(/\[?\]?/g, "")));
Related
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.
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
How do I replace all instances where I have #sometext# in order of the array provided?
The text is a string and my goal is to search for all instances where I have some text surrounded by the "#" character. Then replace those instances with values in the array in the order of the array
Example:
const text = "You are the #RANKED# highest ranked user (out of #TOTALUSER# people)"
const myArray = [10, 50];
After searching and replacing the text should be:
"You are the 10 highest ranked user (out of 50 people)"
text.replace(/#([^#]+)#/g, _ => myArray.shift());
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);
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 4 years ago.
Improve this question
I'm trying to make something that gives an output based on your input. I would like it to take every letter and produce something with the next letter in the alphabet. So if I entered "Hello" it would produce "Ifmmp". The only way I can think to do this is with a series of ifs and else ifs, but I would like to know if there is a faster way.
You can use the String.fromCharCode feature. For example:
let myChar = 'c';
let nextChar = String.fromCharCode(myChar.charCodeAt(0)+1);
console.log(nextChar) // 'd'
In your case, you would replace 'c' with your character variable.
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) })