The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

> Javascript - Find string containing number then duplicate a substring by that number, Any help is greatly appreciated!
Techknowfile
post May 11 2012, 12:25 PM
Post #1





Group: Members
Posts: 4
Joined: 5-February 12
Member No.: 16,405



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

This post has been edited by Techknowfile: May 11 2012, 12:33 PM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
 
Reply to this topicStart new topic
Replies(1 - 1)
Christian J
post May 11 2012, 01:05 PM
Post #2


.
********

Group: WDG Moderators
Posts: 9,630
Joined: 10-August 06
Member No.: 7



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.
User is online!PM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 29th March 2024 - 07:05 AM