Covert a sequence of continuous characters into array in javascript - javascript

I have been trying days to convert a sequence of characters into array .Lets say my sequence would be like var text="abcde"..without any space in the string.All i Wanna do is i wanna convert text into an array like text=[a,b,c,d,e];
Is there any way to do it using javascript .Please Help me

Use split
var array = text.split("");

Use split():
var text = 'abcde',
textArray = text.split(''); // ['a','b','c','d','e']
JS Fiddle demo.
References:
String.split().

Related

Getting strings between two specific occurrences of specific characters in JS

I am working on the following code. How can I extract/get strings between to specific numbers of characters in an string like
lorem1-lorem9-lorem3-lorem8-lorem1-lorem11-one-two-three-lorem22-lorem55.png?
What I need is:
one-two-three
I am able to remove things after the 9 occurrence of the - but not sure how to remove things before the 6 occurrence of - as well
var str = "lorem1-lorem9-lorem3-lorem8-lorem1-lorem11-one-two-three-lorem22-lorem55.png"
console.log(str.split("-", 9).join("-"));
Array.prototype.splice can be used to split an array.
var str = "lorem1-lorem9-lorem3-lorem8-lorem1-lorem11-one-two-three-lorem22-lorem55.png"
let out = str.split("-", 9).splice(6).join("-")
console.log(out);

How to remove strings before nth character in a text?

I have a dynamically generated text like this
xxxxxx-xxxx-xxxxx-xxxxx-Map-B-844-0
How can I remove everything before Map ...? I know there is a hard coded way to do this by using substring() but as I said these strings are dynamic and before Map .. can change so I need to do this dynamically by removing everything before 4th index of - character.
You could remove all four minuses and the characters between from start of the string.
var string = 'xxxxxx-xxxx-xxxxx-xxxxx-Map-B-844-0',
stripped = string.replace(/^([^-]*-){4}/, '');
console.log(stripped);
I would just find the index of Map and use it to slice the string:
let str = "xxxxxx-xxxx-xxxxx-xxxxx-Map-B-844-0"
let ind = str.indexOf("Map")
console.log(str.slice(ind))
If you prefer a regex (or you may have occurrences of Map in the prefix) you man match exactly what you want with:
let str = "xxxxxx-xxxx-xxxxx-xxxxx-Map-B-844-0"
let arr = str.match(/^(?:.+?-){4}(.*)/)
console.log(arr[1])
I would just split on the word Map and take the first index
var splitUp = 'xxxxxx-xxxx-xxxxx-xxxxx-Map-B-844-0'.split('Map')
var firstPart = splitUp[0]
Uses String.replace with regex expression should be the popular solution.
Based on the OP states: so I need to do this dynamically by removing everything before 4th index of - character.,
I think another solution is split('-') first, then join the strings after 4th -.
let test = 'xxxxxx-xxxx-xxxxx-xxxxx-Map-B-844-0'
console.log(test.split('-').slice(4).join('-'))

Javascript get value in attribute

I am using jquery .attr in an input type so when I run this
console.log('name: '+$(this).attr('name'));
output is: name: user_project_category[65][skill][9]
How can I get the 65 and 9?
You can use Regular Expressions to extract text from between the brackets into an array and then you can access the array to get the values you want, you can either extract all text between brackets or just the numbers:
var yourInput = "user_project_category[65][skill][9]";
var allMatches = yourInput.match(/\[(.*?)\]/g);
console.log(allMatches[0]);
console.log(allMatches[2]);
var numberMatches = yourInput.match(/\[([0-9]*?)\]/g);
console.log(numberMatches[0]);
console.log(numberMatches[1]);
var data = "name: user_project_category[65][skill][9]";
console.log(data.split("[")[1].slice(0,-1))//use split to get 65] use slice to remove ]
console.log(data.split("[")[3].slice(0,-1))//use split to get 9] use slice to remove ]
You can use split with slice
Assuming this is not dynamic and format is the same.
for dynamic use regex
Use regex.
var output = 'user_project_category[65][skill][9]';
var numbers = output.match(/\d+/g).map(Number);
alert(numbers);
output: 65,9
Do whatever you want to do with number.
working fiddle
Use regular expression or split function in JavaScript
var output= 'user_project_category[65][skill][9]';
output.split(/(\d+)/)[1];
output.split(/(\d+)/)[3];

js - convert a whole array of strings into a string in which strings are separated by |

I have an array full of strings like [a,b,c,d]. I want to know the efficient way of converting this into 'a|b|c|d' using Javascript.
Thanks.
Pretty simple using Array.prototype.join()
var data = ['a','b','c','d'];
console.log(data.join('|'));
You can use array.join,
var pipe_delimited_= string_array.join("|");
DEMO
var string_array = ['a','b','c','d'];
var pipe_delimited = string_array.join("|");
console.log(pipe_delimited);
Try using array's join() method.The join() method joins array elements into a string.
var arr = ['a','b','c','d'];//your aray
var string =arr.join("|");
console.log(string);
For more see here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join

How can I get words from a string, textarea javascript

I've been working on JavaScript and HTML, I have a text area where the user sets a CSV like this:
17845 hello bye 789
Now I have 17845,hello,bye,789 and I need to extract the values between the commas. I've tried with index Of, but what if the user sets 2 lines instead of 1, how can I get these words? I have thought of separate them getting the "\n".
Javascript function split() will do the trick
var str = '17845,hello,bye,789';
var words = str.split(',');
console.log(words);
Use javascript split() Function
Split function gives u the array.
var sentence ="hello, 123, tedsfd, demo";
var strArr = sentence.split(',');
$.each(strArr,function(key,value){
console.log(value);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
If I understand it correctly, you are facing issues if the user sets comma separated value in multiple lines instead of one... For that use trim() function to remove all tabs and newline and then use split() function.
Use String.split.It converts strings into arrays, given you provide the delimiter, to separate the string.
var userInput = '17845, hello, bye, 789';
data = userInput.split(',');
console.log(data);
//data[0] = '17845'
//data[1] = 'hello'
//data[2] = 'bye'
//...

Categories

Resources