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);
Related
I have a folder path that always starts with a certain string which I want to remove. Let's say it looks like this:
my-bucket/2929023/32822323/file.jpg
I want it to look like this:
2929023/32822323/file.jpg
How would I do that? Thanks!
Using the functions substring and indexOf from String.prototype.
var str = "my-bucket/2929023/32822323/file.jpg";
console.log(str.substring(str.indexOf('/') + 1))
You could use a simple replace method if the string is only present once;
var string = "my-bucket/2929023/32822323/file.jpg";
var revisedString = string.replace('my-bucket/', '');
console.log(revisedString);
However, you're also able to use a Regex (regular expression) to remove it as well, something like;
var string = "my-bucket/2929023/32822323/file.jpg";
console.log(string.replace(/^my-bucket\//, ''));
Use a regex to rip the first one out. No substrings necessary.
var myString= "my-bucket/2929023/32822323/file.jpg";
myString = myString.replace(/^.+?[/]/, '');
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 ..
[^,]*
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];
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('~'))
}
I can't figure out how to search for a string containing something like "[1]", for some reason this doesn't work:
var regExp = '/\[[1-9]\]/';
var search = string.search(regExp); // returns -1
I've searched all over for a solution but can't find anything...
Try it without the '
var regExp = /\[[1-9]\]/;
var search = string.search(regExp);
I think it's the way you're actually attempting to match it. Try this:
string="something[1]";
if(string.match(/\[[1-9]\]/gi)) alert("Your string has brackets with a number inside!"); //Alerts correctly