Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replace function 1

Status
Not open for further replies.

apex82

Programmer
Mar 2, 2009
127
GB
I have the following function that replaces part of a string so that the following:

AA – Test – AA

Becomes:

Test – AA

Code:
function Add () {
       var selObj = document.getElementById('Test1');
       var txtObj = document.getElementById('Test2');

       var selIndex = selObj.selectedIndex;
       var txt = txtObj.value;
       [COLOR=red]txtObj.value = selObj.options[selIndex].text.replace(/\w+[- ]+/,''); [/color]

       }

What I would like is for the function to change the string so it becomes just

Test

Can anyone help?

Thanks.
 
Hi
[ul]
[li]What have you tried so far ?[/li]
[li]What is the criteria for the replacement ?[/li]
[/ul]
With the specification you provided, this would be enough :
Code:
txtObj[teal].[/teal]value [teal]=[/teal] selObj[teal].[/teal]options[teal][[/teal]selIndex[teal]].[/teal]text[teal].[/teal][COLOR=darkgoldenrod]replace[/color][teal]([/teal][fuchsia]/.*/[/fuchsia][teal],[/teal][green][i]'Test'[/i][/green][teal]);[/teal]

Feherke.
 
Each of the “three values” can change so AA – Test – AA might be something like BB – test1 - CC

So in the two examples I above I basically want to be left with Test or test1

The following code replaces the “first value”
Code:
replace(/\w+[- ]+/,'');

so I’m left with Test – AA

I want to replace the “third value” too so I’m left with

Test

Hope that makes sense!

 
Hi

apex82 said:
Hope that makes sense!
Yepp. And my first question ? Was that too uncomfortable ?

I suppose you not tried to apply a similar regular expression :
Code:
[small]txtObj[teal].[/teal]value [teal]=[/teal] selObj[teal].[/teal]options[teal][[/teal]selIndex[teal]].[/teal]text[/small][teal].[/teal][COLOR=darkgoldenrod]replace[/color][teal]([/teal][fuchsia]/\w+[- ]+/[/fuchsia][teal],[/teal][green][i]''[/i][/green][teal])[/teal][highlight][teal].[/teal][COLOR=darkgoldenrod]replace[/color][teal]([/teal][fuchsia]/[- ]+\w+/[/fuchsia][teal],[/teal][green][i]''[/i][/green][teal])[/teal][/highlight]
There was no need to solve it in one step like this :
Code:
[small]txtObj[teal].[/teal]value [teal]=[/teal] selObj[teal].[/teal]options[teal][[/teal]selIndex[teal]].[/teal]text[/small][teal].[/teal][COLOR=darkgoldenrod]replace[/color][teal]([/teal][fuchsia]/\w+[- ]+(\w+)[- ]+\w+/[/fuchsia][teal],[/teal][green][i]'$1'[/i][/green][teal])[/teal]
Personally I would probably prefer this :
Code:
[small]txtObj[teal].[/teal]value [teal]=[/teal] selObj[teal].[/teal]options[teal][[/teal]selIndex[teal]].[/teal]text[/small][teal].[/teal][COLOR=darkgoldenrod]split[/color][teal]([/teal][fuchsia]/[- ]+/[/fuchsia][teal])[[/teal][purple]1[/purple][teal]][/teal]

Feherke.
 
Thank you Feherke!

Really appreciate it. Works perfect.

I need to swot up on regular expressions!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top