8

I need to get the GUID inside [ ] parenthesis. Here is a sample texts:

AccommPropertySearchModel.AccommPropertySearchRooms[6a2e6a9c-3533-4c43-8aa4-0b1efd23ba04].ADTCount

I need to do this with JavaScript using Regular Expressions but so far I am failing. Any idea how I can retrieve this value?

tugberk
  • 57,477
  • 67
  • 243
  • 335

5 Answers5

18

The following regex will match a GUID in the [8chars]-[4chars]-[4chars]-[4chars]-[12chars] format:

/[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}/i

You could find a GUID within square brackets using the following function:

var re = /\[([a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12})\]/i;
function extractGuid(value) {    

    // the RegEx will match the first occurrence of the pattern
    var match = re.exec(value);

    // result is an array containing:
    // [0] the entire string that was matched by our RegEx
    // [1] the first (only) group within our match, specified by the
    // () within our pattern, which contains the GUID value

    return match ? match[1] : null;
}

See running example at: http://jsfiddle.net/Ng4UA/26/

Dan Malcolm
  • 4,382
  • 2
  • 33
  • 27
5

This should work:

str.match(/\[([^\]]+)\]/)

And a version with no regex:

str.substring(str.indexOf('[') + 1, str.indexOf(']'))

I would use the regex, but it may be more convenient for you to use the second version.

deviousdodo
  • 9,177
  • 2
  • 29
  • 34
  • thanks! it worked but retrieves the first `[` with the GUID as well. – tugberk Dec 03 '11 at 14:42
  • As the first element of the array, but the second will contain only what's within the []. – deviousdodo Dec 03 '11 at 14:43
  • @tugberk I've also added an example with no regex. – deviousdodo Dec 03 '11 at 14:51
  • I got it. So, it returns a Array object back. I can retrieve the plain GUID with this code: `str.match(/\[([^\]]+)\]/)[1]` – tugberk Dec 03 '11 at 14:51
  • Yep, the match returns an array. It can be made to return only the match you're interested in, but it would only get more ugly. – deviousdodo Dec 03 '11 at 14:53
  • this is working out for me. A little off topic but does `match` method always returns Array object or it depends on the RegEx? – tugberk Dec 03 '11 at 15:02
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/5553/discussion-between-tugberk-and-draevor) – tugberk Dec 03 '11 at 15:06
  • 1
    `String.match` will always return an array if it matched something, otherwise will return null. Only the array structure will depend on the regex. – deviousdodo Dec 03 '11 at 16:10
4

This worked for me with test GUID

var guid = '530d6596-56c2-4de7-aa53-76b9426bdadc'; // sample GUID
var regex = /^[A-Za-z0-9]{8}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{12}$/i; // validate 8-4-4-4-12
var addDate = function() {
        var newDate = new Date();
        var setGUIDtime = newDate.toString();
        return setGUIDtime;
}; // add date
console.log(guid.match(regex) + ', ' + addDate()); //display/print true GUID with date
1
var testString = "AccommPropertySearchModel.AccommPropertySearchRooms[6a2e6a9c-3533-4c43-8aa4-0b1efd23ba04].ADTCount";
var regex = /\[([a-z0-9\-]+)\]/i;
document.write(testString + "<br/><br/>");
document.write(regex.exec(testString)[1]);

regex.exec(testString)[1] is where the magic happens.

The exec method returns an array with the groups found, where index 0 is the entire match, and 1 is the first group (groups are defined by brackets).

Felix Loether
  • 6,010
  • 2
  • 32
  • 23
f2lollpll
  • 997
  • 6
  • 15
0

This should work

(?<=\[).*?(?=\])

Oops, Javascript does not support lookbehind.

So just use (\[).*?(\]) and remove the leading and trailing characters.

OR

just use (\[)(.*?)(\]) and the 2nd match should have your GUID.

Community
  • 1
  • 1
Ranhiru Jude Cooray
  • 19,542
  • 20
  • 83
  • 128