okay I give up. Here's my code:
var re = /href="(http.*\.jpg)"/g;
var mp3s = body.match(re);
it finds pictures, but it returns href="http://www.picture.com/smthg.jpg"
instead of returning http://www.picture.com/smthg.jpg
any idea why?
The result from match() is actually an object.
I think you need to access the first element on that object.
For example:
body.match(re)[1]
This is where the actual result is kept.
Shameless self-promotion:
I've written a small guide for me, I can never remember how to use these either. It's here: http://queirozf.com/reminders/javascript-regular-expressions-usage-reminder
try
var re = /(http.*\.jpg)/g;
var mp3s = body.match(re);
since you don't need the href.
You want to match the regular expression, but then return just the portion in brackets.
To do this, call the regular expressions exec method. For example:
var body = 'stuff stuff morestuff href="http://www.picture.com/smthg.jpg" and some more stuff';
var re = /href="(http.*\.jpg)"/g;
var regexResults = re.exec(body);
var mp3s = regexResults[1];
alert(mp3s);
Having given you this answer, I must implore you to find a different way to solve this problem. You cannot parse HTML using regular expressions. No matter how sophisticated your regular expression gets, there will be a legal HTML example which will break it.
Related
I am learning JavaScript and I see %value% in a code but I do not know what does it mean or how to use it. Can anyone please help me explain to me. Thank you very much.
var formattedLocation = HTMLworkLocation.replace("%data%", work.jobs[job].location);
"%data%" is just a literal string. This code will take the value of HTMLWorkLocation, look for the first occurrence of %data% in it, and replace that with the value of work.jobs[job].location, and store the resulting string in formattedLocation.
var work = {
jobs: [{
location: "Home office"
}]
};
var job = 0;
var HTMLworkLocation = "John is located at %data%";
var formattedLocation = HTMLworkLocation.replace("%data%", work.jobs[job].location);
console.log(formattedLocation);
This is probably part of a template system that's used to replace placeholders like %data% with values that come from a table.
You're using string.replace which takes a string or regular expression as it's first argument. Based on the code you posted it looks like you're looking for the string "%data%" (or whatever string you're looking for) in HTMLworkLocation and replacing it with the value in work.jobs[job].location. Then it is being stored in formattedLocation.
I would put a debugger; line after that line of code so you can see what the values are in the debugger console. That might help make more sense of things.
Here is more info on the str.replace method with some examples
I have url "SampleProject/profile/aA12". How can I get the value of the id from my rewritten URL using javascript? I want to get the "aA12" value.
Im using htaccess rewrite to rewrite my URL. Im new in rewritting url's. Any help will be appreciated. More powers and thank you.
You can use regex.
Try
'SampleProject/profile/aA12'.match(/\SampleProject\/profile\/(\w+)/)
'SampleProject/profile/aA12/xxx'.match(/\SampleProject\/profile\/(\w+)/)
'aA12' will be matched in both cases.
There are going to be quite a few ways to achieve your goal with JavaScript. A simple solution could be something like this:
let myURL = "SampleProject/profile/aA12";
let result = myURL.split('/').pop();
// returns "aA12"
The .split('/') method is dividing your string up into an array using the / character, and .pop() is simply returning the last element of that array.
Hope this helps! If you were looking for more advanced matching, i.e. if you wanted to ignore a potential query string on the end of the URL parameter, you could use regular expressions.
Their is a many way that you can use to achieve the desired method i made you a code pen in this link
var url = "SampleProject/profile/aA12";
let res = url.split('/').pop();
console.log(res)
https://codepen.io/anon/pen/KQxNja
I'm cobbling together a script which maps mouse X,Y coordinates to an axis grid. The resulting variable will then be passed to CSS transform property. I'm getting all the numbers I need, but I'm stuck on the last part, which is to remove the \ around the result, which has been converted to a regular expression so as to allow for negative integers.
var resultX = RegExp(Math.round(mousePos.x/6.6) -60);
resultX = resultX.replace(/\//g,'');
The final stage (stripping the slashes) throws an error no matter how I do it. I've tried encapsulating .replace in a function, and using return, but I continue to get the same error:
TypeError: 'undefined' is not a function (evaluating
'resultX.replace(///g,'')')
I'm stuck, and haven't been able to find the solution anywhere. Perhaps the problem is that my variable isn't a true string? Or maybe someone has a suggestion for a better way to allow for negative integers.
JS fiddle:
http://jsfiddle.net/wAKnY/
Like JayC mentioned in the comments, there doesn't seem to be a reason to convert to a RegExp in the first place, so I would just recommend removing the enclosing RegExp().
However, if you require that for some reason that isn't apparent here, you can then call toString() on it to enable the replace function to behave correctly:
resultX = resultX.toString().replace(/\//g,'');
As JayC said, you don't need a regexp in the first place.
var resultX = (Math.round(mousePos.x/6.6)-60).toString;
This works:
resultX = resultX.toString().replace(/\//g, '');
resultY = resultY.toString().replace(/\//g, '');
I'm sure there might be something simular on stack overflow but I can't find anything and am getting quite frustrated with what should be very simple.
I need to capture part of a url (similar to a url rewriting engine) using javascript.
URL structure:
http://example.com/constant/CAPTURETHIS
http://example.com/constant/CAPTURETHIS/
http://example.com/constant/CAPTURETHIS#noise
http://example.com/constant/CAPTURETHIS/#noise
I need to just return the CAPTURETHIS text for all 3 senerios
JavaScript supports the retrieval of regex capture-groups by using a string object's match method or a regular-expression object's exec method:
var captureThis = url.match(/^http:[/][/]example[.]com[/]constant[/]([^/]+)/)[1];
var captureThis = /^http:[/][/]example[.]com[/]constant[/]([^/]+)/.exec(url)[1];
But for your example, I almost wonder if it's simpler to use the string object's split method:
var captureThis = url.split(/[/]/)[4];
I want to find anything that comes after s= and before & or the end of the string. For example, if the string is
t=qwerty&s=hello&p=3
I want to get hello. And if the string is
t=qwerty&s=hello
I also want to get hello
Thank you!
\bs=([^&]+) and grabbing $1should be good enough, no?
edit: added word anchor! Otherwise it would also match for herpies, dongles...
Why don't you try something that was generically aimed at parsing query strings? That way, you can assume you won't run into the obvious next hurdle while reinventing the wheel.
jQuery has the query object for that (see JavaScript query string)
Or you can google a bit:
function getQuerystring(key, default_)
{
if (default_==null) default_="";
key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
var qs = regex.exec(window.location.href);
if(qs == null)
return default_;
else
return qs[1];
}
looks useful; for example with
http://www.bloggingdeveloper.com?author=bloggingdeveloper
you want to get the "author" querystring's value:
var author_value = getQuerystring('author');
The simplest way to do this is with a selector s=([^&]*)&. The inside of the parentheses has [^&] to prevent it from grabbing hello&p=3 of there were another field after p.
You can also use the following expression, based on the solution provided here, which finds all characters between the two given strings:
(?<=s=)(.*)(?=&)
In your case you may need to slightly modify it to account for the "end of the string" option (there are several ways to do it, especially when you can use simple code manipulations such as manually adding a & character to the end of the string before running the regex).