Solved

Modifying existing script to add a word before the first word in an input field and add words in the spaces between the other words confused read on...

Posted on 2004-10-13
3
165 Views
Last Modified: 2006-11-17
<script>

function doAndSearch(theForm){
     var sText = theForm.SIMPLE.value.toLowerCase();
     var sText = sText.replace(/<THESAURUS>/gi," ");
     var sText = sText.replace(/\s+/g," ");
     var vWords = sText.split(" ");
     var stringOUT = vWords.join("<THESAURUS>");
     theForm.SIMPLE.value = stringOUT;
               theForm.submit();
              }
</script>
<form NAME="theForm" METHOD="POST" ACTION="/cgi-bin/webcgi.exe">
<INPUT TYPE=TEXT NAME=SIMPLE VALUE='###REPORTED_QUERY###' SIZE=40 style="width:300px">
</form>

I need to modify the above script currently it looks for spaces in the string and then add's a <THESAURUS> between the words and submits the form.

I need to modify this so that

the first word is prefixed by <THESARUS> and other words use the existing logic to add <AND><THESAURUS> so the following

dvd surround sound

will become

<thesaurus>dvd<and><thesaurus>surround<and><thesaurus>sound


thx


0
Comment
Question by:sjsharp80
  • 3
3 Comments
 
LVL 63

Expert Comment

by:Zvonko
ID: 12296503
Does this help:

<script>

var myStr = "dvd surround sound";

var newStr = myStr.split(" ").join("<and><thesaurus>");
if(newStr) newStr = "<thesaurus>"+newStr;


alert(newStr)

</script>

0
 
LVL 63

Expert Comment

by:Zvonko
ID: 12296510
Or one-liner:

<script>

var myStr = "dvd surround sound";

var newStr = "<thesaurus>"+myStr.split(" ").join("<and><thesaurus>");


alert(newStr)

</script>

0
 
LVL 63

Accepted Solution

by:
Zvonko earned 450 total points
ID: 12296532
Or in your form:

<script>

function doAndSearch(theForm){
     var sText = theForm.SIMPLE.value.toLowerCase().replace(/^\s*|\s*$/g,"");
     var sText = sText.replace(/<THESAURUS>/gi," ");
     var sText = sText.replace(/\s+/g," ");
     var vWords = sText.split(" ");
     var stringOUT = vWords.join("<AND><THESAURUS>");
     if(stringOUT>"") stringOUT = "<THESAURUS>"+stringOUT;
     theForm.SIMPLE.value = stringOUT;
     alert(theForm.SIMPLE.value)
}
</script>
<form NAME="theForm" METHOD="POST" ACTION="/cgi-bin/webcgi.exe" onSubmit="doAndSearch(this)">
<INPUT TYPE=TEXT NAME=SIMPLE VALUE='###REPORTED_QUERY###' SIZE=40 style="width:300px">
</form>

0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now