Skip to content

extractNumber — GTM Variable Template for String

VARIABLES › STRING
extractNumber CORE String

Extracts the first number found in a string (including decimals and negatives). Returns undefined if no number is found.


When to Use This

String Manipulation

Transform, clean, and normalize text data for consistent downstream processing.

Extraction

Pull specific values, segments, or patterns from complex data structures.


Examples

Extract integer from text
INPUT
String To Search: The price is 42 dollars
OUTPUT
42
Extract decimal number
INPUT
String To Search: Temperature is 36.5 degrees
OUTPUT
36.5
No numbers returns undefined
INPUT
String To Search: No numbers here
OUTPUT
undefined

GTM Configuration

This is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.

extractNumber
String To Search
💾 The string to search for numbers.
Input Setup
Input Function (optional)
⚙️ Optional pre-processing function applied to the input before internal logic (e.g., convert object to string, normalize case). Internal transformations such as case handling will still apply afterward.
Result Handling
Output Function (optional)
⚙️ Optional function to apply to the result before returning it (e.g., str => str + ' €', val => val !== undefined for boolean conversion). Useful for chaining transformations on the output.
String To Search string
💡 Type any text to see the result update live
🎯 Using special value — click input to type instead
Test with:
Falsy
Truthy
extractNumber()


Under the Hood

📜 View Implementation Code
/**
 * Extracts the first number found in a string.
 *
 * @param {string} data.src - The string to search for numbers.
 * @param {Function|string} [data.out] - Optional output handler: function to transform result or string with format.
 *
 * Direct-mode specific parameters:
 * @param {Function} [data.pre] - Optional pre-processor function to transform src before extracting.
 * 
 * @returns {number|undefined} The first number found, or undefined if no number is found.
 *
 * @framework ggLowCodeGTMKit
 */

const makeNumber = require('makeNumber');

const extractNumber = function(stringToMatch) {
    if (typeof stringToMatch !== 'string') { 
        return undefined; 
    }
    const match = stringToMatch.match("-?\\d+(\\.\\d+)?");
    if (match) {
        return makeNumber(match[0]);
    }
    return undefined;
};

const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);

// ===============================================================================
// extractNumber - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(extractNumber(value));

// ===============================================================================
// extractNumber() – Apply Mode
// ===============================================================================
/*
return function(value) {
   return out(extractNumber(value));
};
*/
🧪 View Test Scenarios (5 tests)
✅ '[example] Extract integer from text'
✅ '[example] Extract decimal number'
✅ String with negative number - should extract negative number
✅ '[example] No numbers returns undefined'
✅ Non-string input - should return undefined