Regex for Site Cifras as CifraClub

6

I need a Regex that works fine, and in JS, to get only the chords of a cipher like this: link

The problem is that I never mess with Regex.

Any ideas?

    
asked by anonymous 14.01.2014 / 01:56

4 answers

3

I have a (free) project that does this. I think you'll like to see the following class. She extracts title, artist, figures etc. link

P.s .: The project name is ToUke: link Complete Code: link .

    
14.01.2014 / 22:59
6

Regex is not the right tool for this problem. By inspecting the source code of the page, I see that the numbers are each in a b element, with class _i0 , _i1 , _i2 , and so on (irrelevant in this particular case). The ideal thing then is to search the DOM for these elements:

var cifras = document.getElementById("ct_cifra")
                     .getElementsByTagName("b");
var texto = "";
for ( var i = 0 ; i < cifras.length ; i++ )
    texto += cifras[i].innerHTML + " ";

If you want to extract these figures directly from the site, you can use a bookmarklet and display the resulting text in alert , console.log , etc. (somewhere you can copy and paste).

javascript:(function(){var cifras=document.getElementById("ct_cifra").getElementsByTagName("b");var texto='';for(var i=0;i<cifras.length;i++)texto+=cifras[i].innerHTML+' ';alert(texto);})();

Note: Successfully tested code in Firefox and Chrome, but in Chrome I was not able to select text from alert dialog box to copy.

    
14.01.2014 / 10:11
2

The following XPath code returns all the digits of the page.

//pre[@id='ct_cifra']/b/text()

Just adapt it to the language you are using.

To test it, in the JS Console of your browser, use the following expression:

$x("//pre[@id='ct_cifra']/b/text()")
    
14.01.2014 / 12:38
1

I'm assuming you refer to Javascript on the client side (browser), not on a server application written in node.js, for example.

StackOverflow in English has a similar question: link

The most voted answer indicates that it is impossible for you to do this using only Javascript, since for security reasons, ajax is only allowed to load pages from the same domain as yours.

To load content from another domain, as is the case with you, you will need a script on the server side (in PHP, Python, Ruby, etc.)

    
14.01.2014 / 10:28