vin
vin

Reputation: 562

Remove common string from a string using jquery or plain Javascript

How can remove the 'tp.' from below string from each place where it is coming

str = " tp.FirstName, tp.FamilyName, tp.DOB, tp.TypeOfLocation
WHERE

tp.DateStamp BETWEEN '2012-02-12 15:13:00' AND '2013-02-12 15:13:00'

AND tp.db_name_id =21
AND

tp.FirstName = 'Darlene'";

I need result as below:

FirstName, FamilyName, DOB, TypeOfLocation
WHERE

DateStamp BETWEEN '2012-02-12 15:13:00' AND '2013-02-12 15:13:00'

AND db_name_id =21
AND

FirstName = 'Darlene'";

Upvotes: 0

Views: 656

Answers (1)

Cerbrus
Cerbrus

Reputation: 72857

Simple:

str = str.replace(/\btp\./g, '');

This uses a regex to search for all occurrences of tp\. in the string, and replaces them with a empty string, effectively removing it.
(The period is escaped, since that's a special character in regexes. It literally searches for tp..
The \b is a word boundary, making sure that tp. is at the start of a word.)

Or, the split/ join method:

str = str.split('tp.').join('');

This will split up the string at every occurrence of 'tp.' (Without copying that along), then join the array together, resulting in a string where 'tp.' was removed.

Upvotes: 5

Related Questions