Remove string quotes from a array javascript? [duplicate] - javascript

This question already has answers here:
Parsing string as JSON with single quotes?
(10 answers)
Convert string into an array of arrays in javascript
(3 answers)
Closed 2 years ago.
I'm trying to remove " " from an array inside a string.
var test = "['a']"
var test1 = "['a','b']"
Expected Output:
var test_arr = ['a']
var test1_arr = ['a','b']
I tried replacing, didn't work
var test_arr = test.replace(/\"/, '');

I see two ways to accomplish that.
JSON.parse('["a","b"]') note that the values need to be in double-quotes.
"['a','b']".replace(/[['\]]/g, '').split(',') note that you need to split after replacing the unwanted chars
Both yield an array containing the original strings.

You can simply convert the single quotes inside the strings to double quotes first to convert the string to a valid JSON, and then we can use JSON.parse to get the required array like:
var test = "['a']"
var test1 = "['a','b']"
var parseStr = str => JSON.parse(str.replace(/'/g, '"'))
var test_arr = parseStr(test)
var test1_arr = parseStr(test1)
console.log(test_arr)
console.log(test1_arr)

Related

Get everything in string after a single comma [duplicate]

This question already has answers here:
How do I split a string with multiple separators in JavaScript?
(25 answers)
How can I convert a comma-separated string to an array?
(19 answers)
Closed 4 years ago.
I have some problem with my string, the variable name is accountcode. I want only part of the string. I want everything in the string which is after the first ,, excluding any extra space after the comma. For example:
accountcode = "xxxx, tes";
accountcode = "xxxx, hello";
Then I want to output like tes and hello.
I tried:
var s = 'xxxx, hello';
s = s.substring(0, s.indexOf(','));
document.write(s);
Just use split with trim.
var accountcode = "xxxx, tes";
var result= accountcode.split(',')[1].trim();
console.log(result);
You can use String.prototype.split():
The split() method splits a String object into an array of strings by separating the string into substrings, using a specified separator string to determine where to make each split.
You can use length property of the generated array as the last index to access the string item. Finally trim() the string:
var s = 'xxxx, hello';
s = s.split(',');
s = s[s.length - 1].trim();
document.write(s);
You can use string.lastIndexOf() to pull the last word out without making a new array:
let accountcode = "xxxx, hello";
let lastCommaIndex = accountcode.lastIndexOf(',')
let word = accountcode.slice(lastCommaIndex+1).trim()
console.log(word)
You can split the String on the comma.
var s = 'xxxx, hello';
var parts = s.split(',');
console.log(parts[1]);
If you don't want any leading or trailing spaces, use trim.
var s = 'xxxx, hello';
var parts = s.split(',');
console.log(parts[1].trim());
accountcode = "xxxx, hello";
let macthed=accountcode.match(/\w+$/)
if(matched){
document.write(matched[0])
}
here \w+ means any one or more charecter
and $ meand end of string
so \w+$ means get all the character upto end of the sting
so here ' ' space is not a whole character so it started after space upto $
the if statement is required because if no match found than macthed will be null , and it found it will be an array and first element will be your match

Split text with {{Text}} format Javascript [duplicate]

This question already has answers here:
Regex to get string between curly braces
(16 answers)
Closed 4 years ago.
Sorry to bother you all. I'm no idea about regular expression. But right now I need one very badly.
I want to split text using this format {{Text}}. The "Text" can be anything. All I need is split the text at the position of {{Text}}.
Here is a sample.
var Regx = My Regx;
var String = "{{This}} is a {{test}} string to be {{spliced}} with {{Regular}} Expression";
var SplitArray = String.split(Regx);
// it will give me an array like this
// ["","is a ","string to be "," with"," Expression"]
Thank you in advance.
Edit:
I solved it myself too. It is {{[^{}]+}}
You can do this way
var test = "{{This}} is a {{test}} string to be {{spliced}} with {{Regular}} Expression";
var SplitArray = test.split(/\{\{.*?\}\}/);
console.log(SplitArray)
Try this:
var Regx = /\{\{.*?\}\}/;
var String = "{{This}} is a {{test}} string to be {{spliced}} with {{Regular}} Expression";
var SplitArray = String.split(Regx);
console.log(SplitArray);
// it will give me an array like this
// ["","is a ","string to be "," with"," Expression"]

Split string with custom condition in javascript (Not Duplicate) [duplicate]

This question already has answers here:
Split a string by commas but ignore commas within double-quotes using Javascript
(17 answers)
Closed 4 years ago.
I have following string :
var str = '15156,"ABALONE, FRIED",60.1,189,19.63,,,,';
i want to split it in the following way :-
[15156, "ABALONE, FRIED", 60.1, 189, 19.63, null, null, null]
i have try this:-
var strArray = str.split(",");
output:-
[15156, "ABALONE", "FRIED", 60.1, 189, 19.63]
How can i get this using javascript's split function or any other way.
Convert that into array like string representation and you can parse it to get the desired output:
var str = '15156,"ABALONE, FRIED",60.1,189,19.63';
var res = JSON.parse('[' + str + ']')
console.log(res);

Split a number from a string in JavaScript [duplicate]

This question already has answers here:
Splitting a string into chunks by numeric or alpha character with JavaScript
(3 answers)
Closed 5 years ago.
I'd like to split strings like
'foofo21' 'bar432' 'foobar12345'
into
['foofo', '21'] ['bar', '432'] ['foobar', '12345']
Is there an easy and simple way to do this in JavaScript?
Note that the string part (for example, foofo can be in Korean instead of English).
Second solution:
var num = "'foofo21".match(/\d+/g);
// num[0] will be 21
var letr = "foofo21".match(/[a-zA-Z]+/g);
/* letr[0] will be foofo.
Now both are separated, and you can make any string as you like. */
You want a very basic regular expression, (\d+). This will match only digits.
whole_string="lasd行書繁1234"
split_string = whole_string.split(/(\d+)/)
console.log("Text:" + split_string[0] + " & Number:" + split_string[1])
Check this sample code
var inputText = "'foofo21' 'bar432' 'foobar12345'";
function processText(inputText) {
var output = [];
var json = inputText.split(' '); // Split text by spaces into array
json.forEach(function (item) { // Loop through each array item
var out = item.replace(/\'/g,''); // Remove all single quote ' from chunk
out = out.split(/(\d+)/); // Now again split the chunk by Digits into array
out = out.filter(Boolean); // With Boolean we can filter out False Boolean Values like -> false, null, 0
output.push(out);
});
return output;
}
var inputText = "'foofo21' 'bar432' 'foobar12345'";
var outputArray = processText(inputText);
console.log(outputArray); Print outputArray on console
console.log(JSON.stringify(outputArray); Convert outputArray into JSON String and print on console

splitting a string based on delimiter [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How do I split a string, breaking at a particular character?
I have a string in following format
part1/part2
/ is the delimiter
now I want to get split the string and get part 1. How can I do it?
result = "part1/part2".split('/')
result[0] = "part1"
result[1] = "part2
split the string and get part 1
'part1/part2'.split('/')[0]
var tokens = 'part1/part2'.split('/');
var delimeter = '/';
var string = 'part1/part2';
var splitted = string.split(delimeter);
alert(splitted[0]); //alert the part1
var result = YourString.split('/');
For your example result will be an array with 2 entries: "part1" and "part2"

Categories