skip to Main Content

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


  1. Chosen as BEST ANSWER

    This work for me;

    let GROUPBY_CAMPOS = TODOS_CAMPOS.map((e)=> {
                
                e = e.replace(' AS DOCUMENTO_MAMA', '')
                e = e.replace(' AS CARNETSALUS_MAMA', '')
                e = e.replace(' AS NOMBRE_MAMA', '')
    
                return e.split(' AS ')[0]
            })
    

  2. 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:

    const input = `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`;
    
    function replaceString(inputString) {
      const replacements = [
        'AS DOCUMENTO_MAMA',
        'AS CARNETSALUS_MAMA',
        'AS NOMBRE_MAMA'
      ];
    
      let output = inputString;
      replacements.forEach(replacement => {
        output = output.replace(replacement, '');
      });
    
      return output;
    }
    
    console.log(replaceString(input));
    Login or Signup to reply.
  3. 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

    const input = `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`;
    
    const replaced = input.replace(/s+ass+[^s,]+(,?)/ig, '$1');
    
    console.log(replaced);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search