Search code examples
javascriptregexstringphotoshop-script

JavaScript: How to get specific text in a string using RegEx


I tummbled into this RegEx and I googled it. A lot. But unfortunately didn't quite understand how RegEx works... So to make this quick since only a tiny winny part of my work requires it so I will be needing you guys. again :))

So here it goes...

All I want is to retrieve a specific string with a format of 0000x0000. For example:

Input:NameName975x945NameName Output: 975x945

Must also consider string like this: NameNameName9751x9451NameNameName (the integer and string are longer...)


Solution

  • Use regex in String.prototype.match() to get specific part of string.

    str.match(/\d+x\d+/)[0]
    

    var str = "NameName975x945NameName";
    var match = str.match(/\d+x\d+/)[0];
    console.log(match)