Replace a Regex Capture Group with Uppercase in JavaScript

Replace a Regex capture group with uppercase in Javascript

You can pass a function to replace.

var r = a.replace(/(f)/, function(v) { return v.toUpperCase(); });

Explanation

a.replace( /(f)/, "$1".toUpperCase())

In this example you pass a string to the replace function. Since you are using the special replace syntax ($N grabs the Nth capture) you are simply giving the same value. The toUpperCase is actually deceiving because you are only making the replace string upper case (Which is somewhat pointless because the $ and one 1 characters have no upper case so the return value will still be "$1").

a.replace( /(f)/, String.prototype.toUpperCase.apply("$1"))

Believe it or not the semantics of this expression are exactly the same.

Javascript replace to uppercase

replace accepts the function as the second parameter, you can use that and return the uppercase result. Also you can modify your regex to accept all vowels. You can use /[aeiou]/g

var str = "aaeeiioouu";str=str.replace(/[aeiou]/g, (c) => {   return c.toUpperCase();});console.log(str);

js replace regex first letter to uppercase

You can use a replacement function:

var xml = '<abc:content><bcd:position></abc:content>';
var xml2 = xml.replace(/:\w/g, function(matched) { return matched.toUpperCase();});
console.log(xml2);

Javascript replace a capture group to uppercase (not a duplicate)

Capture groups are passed as additional arguments to the callback function. So use the argument with the capture.

var text = '<span style="font-variant: small-caps" class="small-caps">Lord</span>'
text = text.replaceAll(/<span style="font-variant.*?>(.*)<\/span>/g,
(match, g1) => g1.toUpperCase());
console.log(text);

Snowflake : REGEXP replace with uppercase of capture group

I am not sure if you can use functions inside REGEXP_REPLACE at all.

Please use the built-in INITCAP function

SELECT INITCAP('Apple,ball,cat', ',');

Reference: INITCAP

Or maybe like this:

SELECT LISTAGG(UPPER(LEFT(VALUE, 1)) || SUBSTRING(VALUE, 2, LEN(VALUE)), ',')
FROM TABLE(SPLIT_TO_TABLE('Apple,ball,cat', ',')) as t(val);

change case with regex

Match the dash followed by a single character, and use a replacer function that returns that character toUpperCase:

const dashToCamel = str => str.replace(/-(\w)/g, (_, g1) => g1.toUpperCase());console.log(dashToCamel("font-size-18"));

how to change uppercase to lowercase with regex javascript

Use /\b[A-Z]{2,}\b/g to match all-caps words and then .replace() with a callback that lowercases matches.

var string = "It's around August AND THEN I get an email",  regex = /\b[A-Z]{2,}\b/g;
var modified = string.replace(regex, function(match) { return match.toLowerCase();});
console.log(modified);// It's around August and then I get an email

How to replace a capture group instead of the full match in a RegEx?

Use a negative lookbehind so that the character isn't included in the match. This will also allow it to match at the beginning of the string (your regexp requires there to be a character before the match).

let x = "the *quick* brown fox";let y = x.replace(/(?<!\\)(\*)(.*)(\*)/g, "<strong>$2</strong>");console.log(y);
x = "the \\*quick* brown fox";y = x.replace(/(?<!\\)(\*)(.*)(\*)/g, "<strong>$2</strong>");console.log(y);


Related Topics



Leave a reply



Submit