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
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 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);
I have a string of the following form:
data-translate='view-7631b26ea80b1b601c313b15cc4e2ab03faedf30'>Avatar data
It can be in different languages, but in any case I need to get a string which is between the characters ' '
That is, in the example above, I need to get the following string:
view-7631b26ea80b1b601c313b15cc4e2ab03faedf30
Can I do this using the method string.replace(regexp, str) ?
I've highlighted the desired line using the following regular expression:
/'\b(.*)\b'/gm
Now, using the method string.replace I need to delete everything except that...
Got any suggestions?
Use match method.
var data = "data-translate='view-7631b26ea80b1b601c313b15cc4e2ab03faedf30'>Avatar data";
data = data.match(/'\b(.*)\b'/gm)
You have good solid anchor text in either side, so:
var match = /data-translate='([^']+)'/.exec(str);
var substr = match && match[1];
Live Example:
var str = "data-translate='view-7631b26ea80b1b601c313b15cc4e2ab03faedf30'>Avatar data";
var match = /data-translate='([^']+)'/.exec(str);
var substr = match && match[1];
document.body.innerHTML =
"<pre>Got: [" + substr + "]</pre>";
But again, as I said in a comment, using a simple regular expression to extract information from HTML is usually doomed to fail. For instance, you probably don't want to match this:
<p>The string is data-translate='view-7631b26ea80b1b601c313b15cc4e2ab03faedf30'</p>
...and yet, a simple regex solution will do exactly that. To properly handle HTML, you must use a proper HTML parser.
You can also try this one:
/\'([^\']+)\'/gm
Code is very simple, but i cant figure out where is the problem:
var str = '789a54bc2';
var matches = str.match(/\d*/);
I expect to see three entries in matches var (789, 54, 2), but there is just first entry (789).
Where is my mistake? Ty!
var str = '789a54bc2';
var matches = str.match(/\d+/g);
Use +, otherwise you'll get empty results in your matches array.
A great reference may be located here and tells you everything you could possibly want to know about RegEx in JavaScript.
This is driving me crazy, what is the reason this doesn't work?
var name = data.match(/first-([A-Za-z0-9-]+)/g).replace('first-', '');
I want to replace first-joe with joe.
I also tried
var name = data.match(/first-([A-Za-z0-9-]+)/g);
var name = name.replace('first-', '');
and that doesn't work.
However the when alerting name I get first-joe
What is the reason for this, and how do I fix it?
Thanks
Try this instead:
var name = data.replace(/first-([A-Za-z0-9-]+)/g, '$1');
match with /g returns an array of matches (excluding parenthesized substrings), so you would have to replace them individually. If you know there is exactly one match, use data.match(/first-([A-Za-z0-9-]+)/)[1] which extracts the parenthesized substring.
I'm not sure why you're calling match first. Why not simply do either of these?
var name1 = data.replace('first-', '');
var name2 = data.replace(/^first-/, ''); //In case somebodys got a name containing first-.
Is it because your data variable contains something more than the string 'first-joe'?