V
V
Vyacheslav Lebedev2016-10-02 22:25:05
JavaScript
Vyacheslav Lebedev, 2016-10-02 22:25:05

regexp. What am I missing?

Good evening!
I'm trying to parse html with a reg, but for some reason characters that should not be there get into the group.
What to fix?
/>(.*?)</gi
And the line is a regular piece of html.
I want to take everything between > and <
I do it like this:

let match =  string.match(/>(.*?)</gi);
match.forEach(e => {
    console.log(e);
})

Thank you!

Answer the question

In order to leave comments, you need to log in

4 answer(s)
V
Vyacheslav Lebedev, 2017-02-04
@slavikse

My solution is the most adequate for my task :)
(.*?)</gi/>

R
romy4, 2016-10-02
@romy4

don't (.*?)
do ([^<]*)

D
Denis, 2016-10-03
@Deonisius

It is very strange that you have never heard that regex is not an HTML parsing tool. No need to try to "disassemble the wheel with a comb."

var htmlString = `
<div id="wrapper">
    <div class="div">Div 1</div>
    <div class="div">Div 2</div>
    <div class="div">Div 3</div>
    <p class="paragraph">P 1</p>
    <p class="paragraph">P 2</p>
    <p class="paragraph">P 3</p>
</div>
`;

// Вариант 1
var parser = new DOMParser(),
    doc = parser.parseFromString(htmlString, 'text/html');
[].forEach.call(doc.querySelectorAll('body *'), function(el){
  console.log(el);
});
// Вариант 2
var tmp = document.createElement('div');
tmp.innerHTML = htmlString;
[].forEach.call(tmp.querySelectorAll('*'), function(el){
  console.log(el);
});

Sandbox example

C
coderisimo, 2016-10-03
@coderisimo

you need to provide all combinations of characters that you need.
for example, there is a newline or spaces.
and if so?
/>((.*?)|(\s+?))</gi

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question