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
166 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…

914 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

17 Experts available now in Live!

Get 1:1 Help Now