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

x
?
Solved

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

Posted on 2014-04-23
3
Medium Priority
?
460 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 1000 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 43

Accepted Solution

by:
David S. earned 1000 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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)

722 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