Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

198
Views
Tener una expresión regular que funcione tanto para Chrome como para Safari

Encontramos algunos problemas en los navegadores Safari con la expresión regular que estamos usando. Esta fue la expresión regular:

/(?<=type: ).*./g

Safari no pudo manejar esto y, por lo tanto, lo cambiamos a esto:

/(?:)(type: ).*./g

El problema con esto es que ahora para Chrome no devuelve el valor correcto. ¿Hay alguna manera de tener una expresión regular que funcione tanto para Chrome como para Safari con la misma respuesta? Entonces la respuesta deseada es:

 BUSINESS ACC false

Ejemplo de código

 const query = "{\n testObject(testInput : {\n id: \"id-13\",\n life: ACC,\n type: BUSINESS,\n promo: [],\n isvalid: false\n })}\n }" const pattern = /\(([^)]+)\)/g; const result = query.match(pattern) ? query.match(pattern).map((v) => v.trim().replace(/^\(|\)$/g, ''))[0] : null; /* DOES NOT WORK IN SAFARI */ const queryTypePattern = /(?<=type: ).*./g; const queryLifePattern = /(?<=life: ).*./g; const queryIsValidPattern = /(?<=isvalid: ).*./g; const queryType = result.match(queryTypePattern) ? result.match(queryTypePattern).map((v) => v.trim().replace(',', ''))[0] : null; const queryLife = result.match(queryLifePattern) ? result.match(queryLifePattern).map((v) => v.trim().replace(',', ''))[0] : null; const queryIsValid = result.match(queryIsValidPattern) ? result.match(queryIsValidPattern).map((v) => v.trim().replace(',', ''))[0] : null; console.log('#### RETURNS THE CORRECT OUTPUT IN CHROME: '); console.log(queryType); console.log(queryLife); console.log(queryIsValid); /* WORKS IN SAFARI */ const queryTypePattern2 = /(?:type: ).*./g; const queryLifePattern2 = /(?:life: ).*./g; const queryIsValidPattern2 = /(?:isvalid: ).*./g; const queryType2 = result.match(queryTypePattern2) ? result.match(queryTypePattern2).map((v) => v.trim().replace(',', ''))[0] : null; const queryLife2 = result.match(queryLifePattern2) ? result.match(queryLifePattern2).map((v) => v.trim().replace(',', ''))[0] : null; const queryIsValid2 = result.match(queryIsValidPattern2) ? result.match(queryIsValidPattern2).map((v) => v.trim().replace(',', ''))[0] : null; console.log('#### RETURNS THE INCORRECT OUTPUT IN CHROME: '); console.log(queryType2); console.log(queryLife2); console.log(queryIsValid2);

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Puede capturar .* y hacer coincidir sin la bandera global.

 /(?:type: )(.*)/

De esta forma, devuelve un array de dos cadenas:

  • valor coincidente completo (por ejemplo, type: BUSINESS )
  • grupo capturado (ej.: BUSINESS )

Necesitamos el segundo.

 const query = "{\n testObject(testInput : {\n id: \"id-13\",\n life: ACC,\n type: BUSINESS,\n promo: [],\n isvalid: false\n })}\n }" const pattern = /\(([^)]+)\)/g; const result = query.match(pattern) ? query.match(pattern).map((v) => v.trim().replace(/^\(|\)$/g, ''))[0] : null; const queryTypePattern2 = /(?:type: )(.*)/; const queryLifePattern2 = /(?:life: )(.*)/; const queryIsValidPattern2 = /(?:isvalid: )(.*)/; const queryType2 = result.match(queryTypePattern2) ? result.match(queryTypePattern2).map((v) => v.trim().replace(',', ''))[1] : null; const queryLife2 = result.match(queryLifePattern2) ? result.match(queryLifePattern2).map((v) => v.trim().replace(',', ''))[1] : null; const queryIsValid2 = result.match(queryIsValidPattern2) ? result.match(queryIsValidPattern2).map((v) => v.trim().replace(',', ''))[1] : null; console.log(queryType2); console.log(queryLife2); console.log(queryIsValid2);

Como la solución no contiene lookbehind, funcionará para ambos navegadores.

about 4 years ago · Juan Pablo Isaza Report

0

Esta respuesta asume que tiene una expresión regular correcta para Chrome y otra expresión regular correcta para Safari, pero la expresión regular que es correcta para un navegador es incorrecta en el otro.

Aquí tienes una función que detecta el navegador:

 function fnBrowserDetect(){ let userAgent = navigator.userAgent; let browserName; if(userAgent.match(/chrome|chromium|crios/i)){ browserName = "chrome"; }else if(userAgent.match(/firefox|fxios/i)){ browserName = "firefox"; } else if(userAgent.match(/safari/i)){ browserName = "safari"; }else if(userAgent.match(/opr\//i)){ browserName = "opera"; } else if(userAgent.match(/edg/i)){ browserName = "edge"; }else{ browserName="No browser detection"; } document.querySelector("h1").innerText="You are using "+ browserName +" browser"; }

Ahora, implementemos una función que use la función de detector de navegador anterior:

 function getRegex() { switch (fnBrowserDetect()) { case "safari": return "yoursafariregex"; case "chrome": return "yourchromeregex"; /* Logic for other browsers if you want to support them*/ } }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!