how to get data from string in javascript - javascript

I have such string test1/test2/test3/test4/test5
How can I get those tests in separate variables or in array or smth using javascript or jquery ?

var arrayOfBits = string.split(separator)

Use split
MN Documentation for split
var data = "test1/test2/test3/test4/test5".split("/");

You could use split (so no jQuery required) -
var arr = "test1/test2/test3/test4/test5".split("/");
console.log(arr);
Demo http://jsfiddle.net/ipr101/hXLE7/

You can use String.split(), where you specify the separator as "/" in the API, and get the array of values in return.

You can split a string by a delimiter.
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/split

Related

Find substring position into string with Javascript

I have the following strings
"www.mywebsite.com/alex/bob/a-111/..."
"www.mywebsite.com/alex/bob/a-222/..."
"www.mywebsite.com/alex/bob/a-333/...".
I need to find the a-xxx in each one of them and use it as a different string.
Is there a way to do this?
I tried by using indexOf() but it only works with one character. Any other ideas?
You can use RegExp
var string = "www.mywebsite.com/alex/bob/a-111/...";
var result = string.match(/(a-\d+)/);
console.log(result[0]);
or match all values
var strings = "www.mywebsite.com/alex/bob/a-111/..." +
"www.mywebsite.com/alex/bob/a-222/..." +
"www.mywebsite.com/alex/bob/a-333/...";
var result = strings.match(/a-\d+/g)
console.log(result.join(', '));
Use the following RegEx in conjunction with JS's search() API
/(a)\-\w+/g
Reference for search(): http://www.w3schools.com/js/js_regexp.asp
var reg=/a-\d{3}/;
text.match(reg);

using regular expression to get strings among commas

I am doing some JavaScript coding and have to process a string to a array.
The original string is this: "red,yellow,blue,green,grey"
What I want to get is an array like this: ["red","yellow","blue","green","grey"]
I have tried to write a function to do this, use indexOf() to get the position of commas then do some further processing. But I think it's to heavy this way. Is there a better way to use regular expression or some existed JavaScript method to implement my purpose?
Thanks all.
use string.split function to split the original string by comma.......
string.split(",")
You can use split:
The split() method splits a String object into an array of strings by separating the string into substrings.
var arr = "red,yellow,blue,green,grey".split(',');
OR
You can also use regex:
var arr = "red,yellow,blue,green,grey".match(/\w+/g);
Try the string.split() method. For further details refer to:
http://www.w3schools.com/jsref/jsref_split.asp
var str = "red,yellow,blue,green,grey";
var res = str.split(",");
you can use .split() function.
Example
.split() : Split a string into an array of substrings:
var str = "red,yellow,blue,green,grey";
var res = str.split(",");
alert(res);
You can use following regular expression ..
[^,]*

Usage of split() API for two special charaters.?

Have code like below
var data = "(5)"
Now, using split i need only number "5", need to truncate the "(" and ")".
If you really want to use split,
var data = "(5)"
alert(data.split(')')[0].split('(')[1])
If you know you're gonna have this pattern (leading + trailing paren), just slice :
"(5)".slice(1, -1);
Don't use split, use replace.
var data - "(5)".replace("(","").replace(")","");

javascript array.toString() element separation

Is there a way to separate array.toString() with semicolons instead of commas?
Check out join(). It takes an argument for the separator.
alert(myArray.join(';'));
Try using the "join" method on the array - array.join(";")
array.toString().replace(/,/g,';');
array.join(';');
var arrayAsString = array.toString();
var whatYouWant = arrayAsString.replace(/,/g, ';');
Though default separator for the join() method is comma(','), you can use other separators also. Refer this JavaScript Array Object : join() Method tutorial.

erase a part of a string in javascript/jquery?

Let's say I have something like this:
var location = '/users/45/messages/current/20/';
and I need to end up with this:
'/45/messages/current/20/'
So, I need to erase the first part of /whatever/
I can use jquery and/or javascript. How would I do it in the best way possible?
To replace everything up to the second slash:
var i = location.indexOf("/", 1); //Start at 2nd character to skip first slash
var result = location.substr(i);
You can use regular expressions, or the possibly more readable
var location = '/users/45/messages/current/20/';
var delim = "/"
alert(delim+location.split(delim).slice(2).join(delim))
Use JavaScript's slice() command. Do you need to parse for where to slice from or is it a known prefix? Depending on what exactly you are parsing for, it may be as simple as use match() to find your pattern.
location.replace("/(whatever|you|want|users|something)/", "");
find the 2nd index of '/' in the string, then substr from the index to the end.
If you always want to eliminate the first part of a relative path, it's probably simplest just to use regular expressions
var loc = '/users/45/messages/current/20/';
loc.replace(/^\/[^\/]+/,'');
location.replace(/\/.*?\//, "/");
location.replace(/^\/[^\/]+/, '')

Categories