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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

cutting out between two words

Status
Not open for further replies.

angelblade27

Programmer
Joined
Jul 30, 2006
Messages
10
Location
US
i want to cut stuff out between two words in a string.
like suppose the string is "hello how are u" i want to pass how and u and have the string be returned as "hello how u". is there a function to do this?
 
well, will it also parse out "are"? you could do this:

Code:
var myStr = "hello how are u";
var newStr = myStr.split("are").join(" ");

alert(newStr);



*cLFlaVA
----------------------------
[tt]somebody set up us the bomb![bomb][/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
make sure you add the spaces in the replace function in cory's example above, otherwise you'll have 3 spaces between the words "how" and "u" (is "u" a word?)

Code:
var myStr = "hello how are u";
var newStr = myStr.split("[COLOR=black red] [/color]are[COLOR=black red] [/color]").join(" ");

alert(newStr);

-kaht

Looking for a puppy?

silky-icon-left.gif
[small]Silky Terriers are small, relatively odorless dogs that shed no fur and make great indoor pets.[/small]
silky-icon-right.gif
 
well it kind of works but i want it to take out everything inbetween. like if it is "hello how are is it u"
everything between how and u should be gone
 
if you want to get technical (and apparently kaht does), you may want to consider a regex, which will strip any number of spaces, not just the hard-coded single-space:

Code:
[COLOR=white red]
var myStr = "hello how                     are        u";
var newStr = myStr.split(/\W+are\W+/g).join(" ");

alert(newStr);[/color]



*cLFlaVA
----------------------------
[tt]somebody set up us the bomb![bomb][/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
[tt]var s="hello how are is it u";
var t=s.replace(/(\bhow\b)(.*?)(\bu\b)/,"$1\x20$3");
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top