Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split variable using a special character in JavaScript

I have a variable var i = "my*text" I want to split it using special character *. I mean, I want to generate var one = "my" and var two = "text" from the above variable.

How can I do this using jQuery and (or) JavaScript? .

like image 860
Alfred Avatar asked Jun 20 '11 11:06

Alfred


People also ask

How do you split a variable in JavaScript?

The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.

How do you use special characters in JavaScript?

In JavaScript you can add special characters to a text string by using the backslash sign.

What is delimiter in JavaScript?

delimiter. Optional. It is delimiter used to break the string into the array of substrings. It can be a single character, string or regular expression. If this parameter is not provided, the split() method will return an array with one element containing the string.

Which method can you use character other than comma to separate values from array?

In that case, the split() method returns an array with the entire string as an element. In the example below, the message string doesn't have a comma (,) character.


2 Answers

values=i.split('*'); one=values[0]; two=values[1]; 
like image 129
Shakti Singh Avatar answered Sep 25 '22 02:09

Shakti Singh


use string.split(separator, limit)

<script type="text/javascript"> var str="my*text"; str.split("*"); </script> 
like image 36
Pranay Rana Avatar answered Sep 23 '22 02:09

Pranay Rana