What does this line of Javascript Regex do? -


the line in question one:

var extendedxmp = (data.match(/xmpnote:hasextendedxmp="(.+?)"/i) || [])[1];

it part of bigger code chunk here:

this.parsecompoundimage = function(data) { var extendedxmp = (data.match(/xmpnote:hasextendedxmp="(.+?)"/i) || [])[1]; if (extendedxmp) {     // need clear out jpeg's block headers. let's juvenile , don't care checking now, shall we?     // 2b + 2b + http://ns.adobe.com/xmp/extension/ + 1b + extendedxmp + 4b + 4b     data = data.replace(new regexp('[\\s\\s]{4}http:\\/\\/ns\\.adobe\\.com\\/xmp\\/extension\\/[\\s\\s]' + extendedxmp + '[\\s\\s]{8}', 'g'), '') }  var xmp = data.match(/<x:xmpmeta [\s\s]+?<\/x:xmpmeta>/g),     result = {} if (!xmp) throw "no xmp metadata found!"; xmp = xmp.join("\n", xmp); 

which comes the source code of depthy app. chunk of code gets xmp metadata , cleans out jpeg exif headers using regex. second line of code confuses me. understand tries match pattern in data, i'm not familiar enough javascript understand it. can explain me line does?

thanks

[1] references first , capture group, (.+?). .match() method returns array consisting of full match (0th element) , capture groups fill other values in array. works way when g flag not set.

|| [] sets extendedxmp empty array when match not found. it's neat feature of javascript , way set variable if evaluation results in "falsey" value, although tend avoid sake of being more obvious , clear in i'm writing. being said, there's no real reason judging code. 1 , concisely avoid whole || [] ordeal , test if (extendedxmp !== null) instead , more "clean", clear, , easier read other developers such or me.


Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -