This question already has answers here:
Parsing string as JSON with single quotes?
(10 answers)
Execute JavaScript code stored as a string
(22 answers)
Closed 1 year ago.
i have a string as below and i would like to extract the array between the quotes.
mystring = "['str1','str2']"
I tried it with eval and i do not want to use eval in my code. is there any other neat way to do this ?
function parseString(string) {
return string
.split(",")
.map((str) => str.replace("[", "").replace("]", "").replaceAll("'", ""));
}
This assumes none of array indexes includes character ",".
Related
This question already has answers here:
Remove characters from a string [duplicate]
(6 answers)
Closed 8 months ago.
I need to write a function that returns a string without the characters 'a' and 's'. For example, the string 'SAverageSsA' will be returned by the function 'verge'. And the string 'AsaAAs' will return an empty string. How can i do this?
let testString = "AsSverage";
function removeAs(string) {}
removeAs(testString);
You could convert the string to an array and filter the elements.
let testString = "AsSverage";
function removeAs(string) {
return Array.from(string).filter((c)=>c!=='a'&&c!='A'&&c!='s'&&c!='S').join('')
}
console.log(removeAs(testString));
This question already has answers here:
How do you split a javascript string by spaces and punctuation?
(4 answers)
How to parse string into words and punctuation marks using javascript
(2 answers)
Closed 9 months ago.
Here I have text something like this:
const string = 'Welcome;;to::';
const string ='Welcome;;';
I want to split this string with ;; and ::;
Expected result:
['Welcome', ';;', 'to', '::']
There will be chance that number of pattern will be increased.
So, is this possible ?
Note: There is no space between words.
This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Javascript Regex: How to put a variable inside a regular expression? [duplicate]
(9 answers)
Closed 2 years ago.
I created a RegExp that works as expected however I can't get it to work in a string format
var regex = new RegExp(/IN1\|2.*\r/g);
The regex is supposed to match the line number which will be taken from variable, in above example it would be line number 2. My question is how do I get it to a string format with a variable inside it?
I tried following but it just doesn't work: "IN1\|" + lineNumber + ".*\x0d//g"
Below is a text in case anyone wants to try:
IN1|**1**||QQ|Noth||||||||20190413|20190413||Self\r
IN1|**2**||QQ|Noth||||||||20190413|20190413||Self\r
IN1|**3**||QQ|LHS||||||||20200506|""||Private|||||||||||||||||||||2342344\r
Thank you.
This question already has answers here:
Get Substring between two characters using javascript
(24 answers)
Closed 4 years ago.
I need to iterate over strings that are inside an array, to get a sub-string in each string.
Substrings are between "()"
Something like this..
let myArray = ["animal(cat)", "color(red)", "fruits(apple)"];
//return
//'cat','red','apple'
How I could do that?
You can do this using substring and lastIndexOf functions.
let myArray = ["animal(cat)", "color(red)", "fruits(apple)"];
myArray.forEach(function(e){
console.log(e.substring(e.lastIndexOf("(") + 1, e.lastIndexOf(")")))
})
This question already has answers here:
How to split comma separated string using JavaScript? [duplicate]
(4 answers)
Closed 5 years ago.
I have a string of ID's wherein they're being separated with commas.
For example, the string is:
"15,14,12,13"
How can I extract the numbers/id's from this string and save each of them in a JSON or array to be something like this
Array: {
15,
14,
12,
13
}
I don't know how it's done using regex or string manipulation. Please advice.
use split & map & parseInt methods.
var numbers="15,14,12,13";
var result=numbers.split(',').map(function(number){
return parseInt(number);
});
console.log('convert '+JSON.stringify(numbers)+" to array:"+JSON.stringify(result));
Use eval method
var numbers="15,14,12,13";
var result=eval("["+numbers+"]");
console.log('convert '+JSON.stringify(numbers)+" to array:"+JSON.stringify(result));