Solved

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

Posted on 2014-04-23
3
453 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
  • 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How can I repeat this modal box over and over again...? 2 24
Jquery 2 23
Web experts ! HTML ! Javascript project for medical MRI sharing 24 49
Dynamic Dropdowns 15 32
Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
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…
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 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…

856 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