Solved

JavaScript/REGEX: remove all words that do not begin with xyz

Posted on 2014-04-23
3
456 Views
Last Modified: 2014-04-23
Using Regular Expressions and JavaScript, how can I remove all words that do not begin with xyz?
var str = 'xyz1 xyz2 xyzzy hello world xyz555 xyzttt';
str = str.replace(???????????????????????????????????);
alert(str); // should be: hello world

Open in new window

0
Comment
Question by:hankknight
[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
  • 2
3 Comments
 
LVL 35

Assisted Solution

by:Terry Woods
Terry Woods earned 250 total points
ID: 40018854
Going by the title of the question:
var str = 'xyz1 xyz2 xyzzy hello world xyz555 xyzttt';
str = str.replace(/\b(?!xyz)\w+/g, '');
alert(str);

Open in new window

Result is:
xyz1 xyz2 xyzzy   xyz555 xyzttt

Open in new window


Going by the comment in your starting code:
var str = 'xyz1 xyz2 xyzzy hello world xyz555 xyzttt';
str = str.replace(/\bxyz\w+/g, '');
alert(str); // should be: hello world

Open in new window

Result is:
hello world

Open in new window

0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 40018861
Let me know if you want the space for any invalid words removed too.
0
 
LVL 42

Accepted Solution

by:
David S. earned 250 total points
ID: 40018874
You may be other-thinking it, Terry. You do want to match the "xyz". Also "\b" won't work as intended here because, at least in JavaScript RegEx, a hyphen is counted as a word-boundary.
str = str.replace(/(^|\s+)xyz\S*/g, '');

Open in new window

0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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…

728 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