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

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(")","");

Related

How to use split() to convert 1.18.0-AAA-1 into 1.18.0 js

How to use the javascript split splice slice methods to convert the:
1.18.0-AAA-1 into 1.18.0.
Start with the initial value, determine that the portion you want is before the first hyphen, so use that as the delimiter for the split. Perform the split and then the first portion will be everything up to but not including that first hyphen. You don't need slice or splice for this - just split. Then just add the dot at the end for the trailing dot.
var x="1.18.0-AAA-1";
var y=x.split("-");//splits it at each "-";
var z=y[0]+".";//gives 1.18.0.
however if you are asking to use each of the threeemethods to yield the outcome, then this sounds like homework and you should try doing it on your own. Best way to learn is to try.
Use split to create an array from your string
var str = "1.18.0-AAA-1";
var parts = str.split("-"); // this returns the array ["1.18.0", "AAA", "1"]
Now the easiest way to get what you want is doing:
parts[0];

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);

String split with multiple separators

I'm trying to split a huge string that uses "}, {" as it's separator.
If I use the following code will I get split it into it's own string?
var i;
var arr[];
while(str) {
arr[i] = str.split("/^}\,\s\{\/");
}
First, get rid of the while loop. Strings are immutable, so it won't change, so you'll have an infinite loop.
Then, you need to get rid of the quotation marks to use regex literal syntax and get rid of the ^ since that anchors the regex to the start of the string.
/},\s\{/
Or just don't use a regex at all if you can rely on that exact sequence of characters. Use a string delimiter instead.
"}, {"
Also, this is invalid syntax.
var arr[];
So you just do the split once, and you'll end up with an Array of strings.
All in all, you want something like this.
var arr = str.split(/*your split expression*/)
var arr = str.split(/[\{\},\s]+/)
var s = 'Hello"}, {"World"}, {"From"}, {"Ohio';
var a = s.split('"}, {"');
alert(a);

how to get data from string in 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

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