Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
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
Medium Priority
?
172 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1800 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…

715 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