Printable Version of Topic

Click here to view this topic in its original format

HTMLHelp Forums _ Client-side Scripting _ Javascript - Find string containing number then duplicate a substring by that number

Posted by: Techknowfile May 11 2012, 12:25 PM

I am trying to help force an elementary search engine (which ranks its results solely by the amount of instances the searched term is found in each html page) into allowing me to influence the search results with keywords. The keywords are included in the body of the html, and are each in a paragraph with a keywords class applied. The keywords are then hidden using css.

css

CODE
//Hide any text with class="example" from display
.example{
    display: none;
}


HTML Excerpt
CODE
<p class="example">red, green[25], blue</p>


I would like to use javascript to find each string in an example class paragraph that contains a number inside of brackets at the end (eg green[25]) and replace that string with the substring found before the opening bracket printed the amount of times as the number inside of the bracket, which adding a space to the start and comma to the end of each duplicate.

for example, the HTML Excerpt above would be changed to:
CODE
<p class="example">red, green, green, green, green, green, green, green, green, green, green, green, green, green, green, green, green, green, green, green, green, green, green, green, green, green, blue</p>


I do have some experience with regular expressions in SED, which I believe may help with this matter. I've simply never used regex in javascript before -_-;

Can anyone point me in the right direction as to how this can be accomplished? I appreciate any help you can prove smile.gif

Posted by: Christian J May 11 2012, 01:05 PM

QUOTE(Techknowfile @ May 11 2012, 07:25 PM) *

I am trying to help force an elementary search engine (which ranks its results solely by the amount of instances the searched term is found in each html page) into allowing me to influence the search results with keywords.

First of all, can this search engine run javascript? If not, JS-generated text will not be visible to it.

QUOTE
I would like to use javascript to find each string in an example class paragraph that contains a number inside of brackets at the end (eg green[25]) and replace that string with the substring found before the opening bracket printed the amount of times as the number inside of the bracket, which adding a space to the start and comma to the end of each duplicate.

Why not just make the original HTML text content look like that from the start? Or, if you need to change existing content, maybe some advance text editors can perform such operations too (however I don't have experience with that). unsure.gif

QUOTE
I do have some experience with regular expressions in SED, which I believe may help with this matter. I've simply never used regex in javascript before -_-;

Can anyone point me in the right direction as to how this can be accomplished? I appreciate any help you can prove smile.gif

Instead of using regex I'd make an array of all words with split(), then if a word ends with a ] bracket use split again to isolate the number after the [ bracket, finally multiply the word with the number (say in a loop), replace the old word with the string of new ones, turn the array back into a string again with join(), etc.

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)