please help me,
I have a string that corresponds to a SQL query;
let string = `SPMAMA.DOCUMENTO AS DOCUMENTO_MAMA,
NVL(SPMAMA.CARNETSALUS,SPMAMA_N.CARNETSALUS) AS CARNETSALUS_MAMA,
NVL(SPMAMA.NOMBRE1 || ' ' || SPMAMA.NOMBRE2 || ' ' || SPMAMA.APELLIDO1 || ' ' || SPMAMA.APELLIDO2,
SPMAMA_N.NOMBRE1 || ' ' || SPMAMA_N.NOMBRE2 || ' ' || SPMAMA_N.APELLIDO1 || ' ' || SPMAMA_N.APELLIDO2) AS NOMBRE_MAMA`;
and I want to remove the alias "AS NAME_ALIAS". Something like this:
let strWithoutAlias = `SPMAMA.DOCUMENTO,
NVL(SPMAMA.CARNETSALUS,SPMAMA_N.CARNETSALUS),
NVL(SPMAMA.NOMBRE1 || ' ' || SPMAMA.NOMBRE2 || ' ' || SPMAMA.APELLIDO1 || ' ' || SPMAMA.APELLIDO2,
SPMAMA_N.NOMBRE1 || ' ' || SPMAMA_N.NOMBRE2 || ' ' || SPMAMA_N.APELLIDO1 || ' ' || SPMAMA_N.APELLIDO2)`;
I don’t know how to use split and slice to do this, as I think it would be a combination of both.
This is my silly code;
let strngSplit = `SPMAMA.DOCUMENTO AS DOCUMENTO_MAMA,
NVL(SPMAMA.CARNETSALUS,SPMAMA_N.CARNETSALUS) AS CARNETSALUS_MAMA,
NVL(SPMAMA.NOMBRE1 || ' ' || SPMAMA.NOMBRE2 || ' ' || SPMAMA.APELLIDO1 || ' ' || SPMAMA.APELLIDO2,
SPMAMA_N.NOMBRE1 || ' ' || SPMAMA_N.NOMBRE2 || ' ' || SPMAMA_N.APELLIDO1 || ' ' || SPMAMA_N.APELLIDO2) AS NOMBRE_MAMA`.replace(/r?n|r/g, '').split('AS')
3
Answers
This work for me;
As of my understanding, you don’t need a regex for this. You can directly use the string
replace()
function and proceed ahead to remove the specified blocks of string from your original string.Refer the below code for reference:
You could use a regex to remove all aliases:
/s+ – white spaces before AS
as – AS
s+ – white spaces after AS
[^s,]+ – alias, means anything but white spaces and a comma
(,?) – capture a comma in the end if exists to replace with
/ig – case insensitive + all occurences