Using exec() to Match a String - javascript

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

Related

Regex to match parmaters of function

I have the following text on my page:
pageTracker._addItem("2040504","JACQXSPINKASS-TX4-8","Jacq Socks","","9.00000","1.0");
pageTracker._addItem("2040504","FTWCLSNOCOLOURONE SIZE","Footwear Cleaner","","8.00000","1.0");
I would like to just extract the parameters that are within the brackets for each line using javascripts match() function. I have the following regex but it's not quite right:
/\b_addItem[^);]+/g
This matches the _addItem( part as well. How can I tweak this to only get the stuff inside the brackets?
Regexr example
Ideally it should match any string that begins with pageTracker._addItem(" but not include that part in the match up to the closing bracket.
I am going to be doing the matching with javascript with I don't think supports look behinds if I'm right
Use a look behind to assert, but not capture, the preceding text:
/(?<=pageTracker\._addItem\()[^);]+/g
Note that I added ( to the look behind to not capture that either.
Now that you've added the JavaScript tag, where look behinds are not supported, you must capture your target in a group:
/pageTracker\._addItem\(([^);]+)/g
Your target will be in group 1.
You can split it into two regular expression calls to avoid look-behind:
var str = 'pageTracker._addItem("2040504","JACQXSPINKASS-TX4-8","Jacq Socks","","9.00000","1.0");\npageTracker._addItem("2040504","FTWCLSNOCOLOURONE SIZE","Footwear Cleaner","","8.00000","1.0");'
var m,output=[];
var re = /^pageTracker._addItem\("(.*)"\)/gm;
while(m=re.exec(str))
output.push(m[1].split('","'));
Output is then a 2D array:
[
["2040504","JACQXSPINKASS-TX4-8","Jacq Socks","","9.00000","1.0"],
["2040504","FTWCLSNOCOLOURONE SIZE","Footwear Cleaner","","8.00000","1.0"]
]
You might do as follows too
var strings = ['pageTracker._addItem("2040504","JACQXSPINKASS-TX4-8","Jacq Socks","","9.00000","1.0");',
'pageTracker._addItem("2040504","FTWCLSNOCOLOURONE SIZE","Footwear Cleaner","","8.00000","1.0");'
],
args = strings.map(s => s.match(/".*?"/g)
.map(s => s.replace(/"/g,'')));
console.log(args);

Trying to use Replace to replace background-position part with Regex to parse URL

I'm trying to replace this string url(http://www.yahoo.com/someimage)
var url = $(someelement).css("background-image"); //url(http://www.yahoo.com/someimage)
var path = url.replace(/url(\()|(\))/, '');
I have tried a few ways but I can't seem to get the end parenthesis. I am not sure what I need to do to write this correctly.
Match the ^start and end$ of the string, and let it know it is global for two matches;
/^url\(|\)$/g
You could also make it optionally look for quotes like this
/^url\(['"]?|['"]?\)$/g

Parse string regex for known keys but leave separator

Ok, So I hit a little bit of a snag trying to make a regex.
Essentially, I want a string like:
error=some=new item user=max dateFrom=2013-01-15T05:00:00.000Z dateTo=2013-01-16T05:00:00.000Z
to be parsed to read
error=some=new item
user=max
dateFrom=2013-01-15T05:00:00.000Z
ateTo=2013-01-16T05:00:00.000Z
So I want it to pull known keywords, and ignore other strings that have =.
My current regex looks like this:
(error|user|dateFrom|dateTo|timeFrom|timeTo|hang)\=[\w\s\f\-\:]+(?![(error|user|dateFrom|dateTo|timeFrom|timeTo|hang)\=])
So I'm using known keywords to be used dynamically so I can list them as being know.
How could I write it to include this requirement?
You could use a replace like so:
var input = "error=some=new item user=max dateFrom=2013-01-15T05:00:00.000Z dateTo=2013-01-16T05:00:00.000Z";
var result = input.replace(/\s*\b((?:error|user|dateFrom|dateTo|timeFrom|timeTo|hang)=)/g, "\n$1");
result = result.replace(/^\r?\n/, ""); // remove the first line
Result:
error=some=new item
user=max
dateFrom=2013-01-15T05:00:00.000Z
dateTo=2013-01-16T05:00:00.000Z
Another way to tokenize the string:
var tokens = inputString.split(/ (?=[^= ]+=)/);
The regex looks for space that is succeeded by (a non-space-non-equal-sign sequence that ends with a =), and split at those spaces.
Result:
["error=some=new item", "user=max", "dateFrom=2013-01-15T05:00:00.000Z", "dateTo=2013-01-16T05:00:00.000Z"]
Using the technique above and adapt your regex from your question:
var tokens = inputString.split(/(?=\b(?:error|user|dateFrom|dateTo|timeFrom|timeTo|hang)=)/);
This will correctly split the input pointed out by Qtax mentioned in the comment: "error=user=max foo=bar"
["error=", "user=max foo=bar"]

Javascript regular expression - get all text after [stuff in here]

I have strings in my program that are like so:
var myStrings = [
"[asdf] thisIsTheText",
"[qwerty] andSomeMoreText",
"noBracketsSometimes",
"[12345]someText"
];
I want to capture the strings "thisIsTheText", "andSomeMoreText", "noBracketsSometimes", "someText". The pattern of inputs will always be the same, square brackets with something in them (or maybe not) followed by some spaces (again, maybe not), and then the actual text I want.
How can I do this?
Thanks
One approach:
var actualTextYouWant = originalString.replace(/^\[[^\]]+\]\s*/, '');
This will return a copy of originalString with the initial [...] and whitespace removed.
This should get you started:
/(?:\[[^]]*])?\s*(\w+)/

Struggling with regex in javascript to take a matched part of a string and use that to replace another part

I have an expression in the middle of a bunch of html (actually an rss feed) of the form
by unknownstring
where unknownstring can be any string containing alphanumerics, including dashes, but it won't contain spaces nor slashes. Effectively, it's a directory name.
I need to be able to change it to:
by unknownstring
I've been reading up about regex but most of it's going over my head. Unfortunately I don't understand enough to make sense of most of what I've read here and in other places.
Help much appreciated :)
Try this one:
str = 'by unknownstring'
regex = /\<a href="javascript:\/\/"\>by (\w+)\<\/a\>/
replacement = 'by $1'
console.log(str.replace(regex, replacement));
// by unknownstring
Here's some material that I used. :)
Using different match pieces where you substitute one match piece for another, you can do it like this:
var html = 'by unknownstring';
html = html.replace(/(<a\s+.*?href=['"])(javascript:\/\/)(.*?>by\s+)(.*?)(<\/a>)/g, "$1$4$3$4$5");
alert(html);
You can see this one work here: http://jsfiddle.net/jfriend00/KgN7t/. Conceptually, we write a regex to match all the different pieces of the match string and then we replace the whole thing while substituting one piece for another. Each piece in the regex is delineated with parens.
Or you can use a similar concept, but with a custom replace function like this:
var html = 'by unknownstring';
html = html.replace(/<a\s+.*?href=['"](javascript:\/\/).*?>by\s+(.*?)<\/a>/g, function(str, p1, p2) {
return(str.replace(p1, p2));
});
You can see it work here: http://jsfiddle.net/jfriend00/ewnqj/
Regex it like this:
var str = 'by unknownstring';
var regx = 'by (.*)';
var output = 'by $1';
str.replace(RegExp(regx),output);
Enjoy!

Categories