Trying to utilize Javascript’s String.prototype.replace() function in Snowflake, because apparently Snowflake’s regex_replace
function lacks certain features such ast (forward/backward) look arounds.
Here is my Javascript UDF attempt:
JavaScript
x
11
11
1
CREATE OR REPLACE FUNCTION REXP_REPLACE_ME(subject TEXT, pattern TEXT, replacement TEXT)
2
RETURNS string
3
LANGUAGE JAVASCRIPT
4
AS
5
$$
6
const p = SUBJECT;
7
const regex = PATTERN;
8
return p.replace(regex, REPLACEMENT);
9
$$
10
;
11
However, when I try to execute using the example provided in the string.prototype.replace()
documentation linked above. I’m not getting the expected result:
JavaScript
1
5
1
SELECT REXP_REPLACE_ME('The quick brown fox jumps over the lazy ferret. If the dog reacted, was it really lazy?', '/Dog/i', 'cat')
2
3
//expected output: 'The quick brown fox jumps over the lazy ferret. If the cat reacted, was it really lazy?'
4
//actual output: 'The quick brown fox jumps over the lazy ferret. If the dog reacted, was it really lazy?'
5
Any ideas?
Advertisement
Answer
because in Javascript the regex is not string lateral, it is it’s own thing.
JavaScript
1
8
1
> a = 'The quick brown fox jumps over the lazy ferret. If the dog reacted, was it really lazy?';
2
'The quick brown fox jumps over the lazy ferret. If the dog reacted, was it really lazy?'
3
> b = a.replace('/Dog/i', 'cat');
4
'The quick brown fox jumps over the lazy ferret. If the dog reacted, was it really lazy?'
5
> b = a.replace(/Dog/i, 'cat');
6
'The quick brown fox jumps over the lazy ferret. If the cat reacted, was it really lazy?'
7
>
8