M
M
Muranx2020-04-28 05:39:36
JavaScript
Muranx, 2020-04-28 05:39:36

Why doesn't the regular expression match in this case?

Hello!

let n = 12.355; console.log(n.toFixed(2))
let n = 12.355 ; console.log(n.toFixed(5));
console.log(12.toFixed(5));
console.log(12..toFixed(5));
console.log(1e3); // 1000
console.log(5e-2);
console.log(1e500);
console.log(0/0);
console.log(1/0);
console.log(isNaN("12"));
console.log(NaN !== 0/0);
console.log(typeof +"Infinity");
console.log(+"12px");
console.log(typeof +"\n12");
console.log(+"1 2");
console.log(+"1, 2");

fc = document.querySelectorAll('.forCode'); // это коллекция всех див блоков, в каждом из которых содержится код представленный в хтмл

let regObj = {
  'console' : '<span style="color: red;">',
  'isNaN' : '<span style="color: red;">',
  'toFixed' : '<span style="color: red;">',
  'Infinity' : '<span style="color: red;">',
  'let' : '<span style="color: #7592c7;">',
  'typeof' : '<span style="color: #7592c7;">',
  '".+"' : '<span style="color: green;">', // ПОЧЕМУ не мэтчится?
};

for(let p in regObj){
  for(let k=0; k<fc.length; k++){
    fc[k].innerHTML = fc[k].innerHTML.replace(p, m => regObj[p]+m+'</span>');
  };
};

There is an array htmlof elements on the page, there is an object whose key is a "regular expression" and the value is what will be substituted into the function replace( )for replacement. I need this to highlight the standard syntax JSin the document, BUT for some reason I can't replace the // ПОЧЕМУ не мэтчится?. Although if you check, it works correctly, and returns ! How can this problem be eliminated in this implementation, and highlight the lines in the code with the standard green color for many sites? Thank you! ps: you need exactly all the info in quotes (+ the quotes themselves), and not just , well, in general, those who answer this question themselves are aware that all information in the lowercase version is highlighted in green on most sites! /".+"/gim.test('isNaN("12")')truehtml
\w+

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ilya, 2020-04-28
@rpsv

Again? Seriously?! Is this some kind of coursework? Example: https://regex101.com/r/Lc2DmI/1
const regex = /"(.+?)"/gs;

A
Arthur, 2020-04-28
@ar2rsoft

You can do this. This will create a regular expression from your string and it will already work new RegExp(<тут ваша рега>)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question