using regular expression to get strings among commas - javascript

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 ..
[^,]*

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

need regex to get string before and after sepearted by a colon

I am having strings like following in javascript
lolo dolo:279bc880-25c6-11e3-bc22-3c970e02b4ec
i want to extract the string before : and after it and store them in a variable. I am not a regex expert so i am not sure how to do this.
No regex needed, use .split()
var x = 'lolo dolo:279bc880-25c6-11e3-bc22-3c970e02b4ec'.split(':');
var before = x[0]
var after = x[1]
Make use of split(),No need of regex.
Delimit your string with :,So it makes your string in to two parts.
var splitter ="lolo dolo:279bc880-25c6-11e3-bc22-3c970e02b4ec".split(':');
var first =splitter[0];
var second =splitter[1];

Find and Replace using Regular Expression

Well the answer should be very simple.But i am new to the regular expression.
What i want to do is just find and replace :
Eg: iti$%#sa12c##ombina#$tion.43of//.45simp5./l7e5andsp75e$%cial23$#of%charecters
In the above sentence replace the words "of" with "in"
I tried this but didn't get the result, please help me out.
string="iti$%#sa12c##ombina#$tion.43of//.45simp5./l7e5andsp75e$%cial23$#of%charecters";
var string2=string.replace("/(\w*\W*)of(\w*\W*)/g","$1in$2");
console.warn(string2);
Fix the regex literal (no quotes) and use word boundaries (\b, no need to use $1 and $2) :
var string2 = string.replace(/\bof\b/g, "in");
Why not a simple var replaced = yourString.replace(/of/g, 'in');?
Globally replace without using a regex.
function replaceMulti(myword, word, replacement) {
return myword.split(word).join(replacement);
}
var inputString = 'iti$%#sa12c##ombina#$tion.43of//.45simp5./l7e5andsp75e$%cial23$#of%charecters';
var outputString = replaceMulti(inputString, 'of', 'in');
Like this?
str.replace("of","in");
Regular expressions are literals or objects in JavaScript, not strings.
So:
/(\w*\W*)of(\w*\W*)/g
or:
new Regexp("(\\w*\\W*)of(\\w*\\W*)","g");

Splitting and returning part of a string

I have two strings such as:
sometext~somemoretext~extratext
and
sometext~otherextratext
I wish to crop off the last tilde (~) and all text to the right. For instance, the above two strings would result in:
sometext~somemoretext
and
sometext
Thanks
lastIndexOf(char) returns the position of the last found occurrence of a specified value in a string
substring(from, to) extracts the characters from a string, between two specified indices, and returns the new sub string
For instance:
var txt = 'sometext~somemoretext~extratext';
txt = txt.substring(0, txt.lastIndexOf('~'));
DEMO
I strongly suggest you to read the doc on the Javascript String Object
return theString.replace(/~[^~]*$/, '');
You can do this using a regular expression with the .replace() DOCs method.
var str = 'sometext~somemoretext~extratext';
str = str.replace(/~[\w\s]+$/, '');
Here is a jsFiddle of the above code for you to run: http://jsfiddle.net/NELFB/
you can use substr to split the string, then rebuild them for what ever you need
var someString = "sometext~otherextratext";
someString = someString.split('~');
this will give you an array, which you can use like someString[0];
use .replace('~', '') if you need to further remove the ones at the end of strings
this should do it
function removeExtra(input){
return input.substr(0,input.lastIndexOf('~'))
}

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

Categories