I've got a string 'url(data:image/png;base64,iVBORw0K...GgoA)'.
I need to invoke only base64 data from it. In output i'd like to see something like this ['iVBORw0K...GgoA'].
Could anyone help me with creating a correct RegExp expression?
Thanks in advance.
.*base64,(\w+)\)$
If you get group 1 from the regex, you will get the base64 data you want.
You can write a regular expression like this,
var phrase = "url(data:image/png;base64,iVBORw0K...GgoA)";
var myRegexp = /base64,(.*)/;
var match = myRegexp.exec(phrase);
alert(match[1]);
HTH
Related
Given
google.com
How can I get .com and save it to a variable. I was thinking of using regex to split google.com into google and ".com", but I don't know the regex to do this.
It might help to know that I got the hostname from using window.location.hostname
Thank you!
You can split the location by ., and the last value in the array will be the extension. Something like this would work:
var extension=location.hostname.split(".");
extension=extension[extension.length-1];
console.log(extension)
JS Fiddle Example: https://jsfiddle.net/igor_9000/yvy0zrat/
Hope that helps!
You could use a substring and lastIndexof:
var str = window.location.hostname;
var ending = str.substring(str.lastIndexOf(".")+1);
https://jsfiddle.net/06r86q0n/
Mostly the same as the other answer, but neither helps with double domains like ".co.uk"
If you are looking to create a regex statement that will only grab the .com then I would use something like:
/(.com)/ as your Regex statement
so your code would look like something like this:
var regexMatch = /(.com)/; as a regular expression literal
or
var regexMatch = new RegExp(".com"); as constructor function of the RegExp object.
Do not use split, its a bit slower than slice:
var last = location.hostname
var t = last.slice(last.lastIndexOf("."));
console.log(t) // .com, .net etc...
Fiddle: https://jsfiddle.net/1qu0rffb/
I got this
'','','{12345678},{87654321}','lk12l1k2l12lkl12lkl121l2lk12'
trying to match it using '(.*?)',|'(.*?)'
It successfully got my 4 chunks
''
''
'{12345678},{87654321}'
'lk12l1k2l12lkl12lkl121l2lk12'
But I am trying to use the same regex in split... it doesn't like it. :(
var str = "'','','{12345678},{87654321}','lk12l1k2l12lkl12lkl121l2lk12'";
str.split(/'(.*?)',|'(.*?)'/);
Any idea...? ugh.
Why are you using split?
You can get your four chunks with match:
var chunks = str.match(/'[^']*'/g);
Is the split() neccessary?
You could always get the information between the quotes using match().
test.match(/'(.*?)'/g)
Test it please.
I am desperate - I don't see what I'm doing wrong. I try to replace all occurrences of '8969' but I always get the original string (no matter whether tmp is a string or an int). Maybe it's already too late, maybe I'm blind, ...
var tmp = "8969";
alert("8969_8969".replace(/tmp/g, "99"));
Can someone help me out?
The / characters are the container for a regular expression in this case. 'tmp' is therefore not used as a variable, but as a literal string.
var tmp = /8969/g;
alert("8969_8969".replace(tmp, "99"));
alert("8969_8969".replace(/8969/g, "99"));
or
var tmp = "8969"
alert("8969_8969".replace(new RegExp(tmp,"g"), "99"));
Live DEMO
Dynamic way of handling a regex:
var nRegExp = new RegExp("8969", 'g');
alert("8969_8969".replace(nRegExp, "99"));
/tmp/g. This is a regex looking for the phrase "tmp". You need to use new RegExp to make a dynamic regex.
alert("8969_8969".replace(new RegExp(tmp,'g'), "99"));
Javascript doesn't support that usage of tmp, it will try to use 'tmp' literally, as a regex pattern.
"8969_8969".replace(new RegExp(tmp,'g'), "99")
I have a string, which I want to extract the value out. The string is something like this:
cdata = "![CDATA[cu1hcmod6rbg3eenmk9p80c484ma9B]]";
And I want cu1hcmod6rbg3eenmk9p80c484ma9B. In other words, I want anything inside the ![[CDATA[*]].
I tried to use the following javascript snippet:
cdata = "![CDATA[cu1hcmod6rbg3eenmk9p80c484ma9B]]";
rePattern = new RegExp("![?:\\s+]]","m");
arrMatch = rePattern.exec( cdata );
result = arrMatch[0];
But the code is not working, I'm pretty sure that it's the way I how specify the matching string that's causing the problem. Any idea how to fix it?
Your pattern should be something like...
/^!\[CDATA\[(.+?)\]\]$/
Which is...
Match literal starting ![CDATA[.
Lazy match everything up until the closing ] and save it in capturing group $1 (thanks Phrogz for his excellent suggestion).
Match extra ]].
Your string should be available as arrMatch[1].
Try this:
var cdata = "![CDATA[cu1hcmod6rbg3eenmk9p80c484ma9B]]";
var regPattern = /(.*CDATA\[)(.*)(\]\].*)/gm;
alert(cdata.replace(regPattern, "$2"));
i want to extract 34 from this string. How Can i done that ? (i will use javascript)
#project_maincategory=3&project_subcategory=34&project_tags[]=70&project_tags[]=71&created_in=30
var src = "#project_maincategory=3&project_subcategory=34&project_tags[]=70&project_tags[]=71&created_in=30",
match = /project_subcategory=(\d+)/g.exec(src);
alert(match[1]);
Anyways, it looks like a query string so there should be a better way to parse/read that string. See http://blog.falafel.com/Blogs/AdamAnderson/07-12-17/Parse_a_Query_String_in_JavaScript.aspx
.*project_subcategory=(\d*).*