Solved

Convert PHP preg_replace code to JavaScript replace

Posted on 2011-02-21
1
515 Views
Last Modified: 2012-05-11
I would like this PHP REGEX to be converted to JavaScript.

I only want the extra whitespace to be replaced with a newline if the whitespace already contains a newline.

If extra whitespace does NOT contain a newline then the extra whitespace should be replaced with a space.

It works correctly in PHP but I need it converted to JavaScript.

Thanks!
<pre><?php

$xyz='Hello,     world.
This      is	 at test
               and only a          test';
echo preg_replace("/(?=\s\s)\s*?(\n)\s*|(\s)\s+/", "$1$2", $xyz);

?></pre>

<script type="text/javascript">

var xyz = 'Hello,     world. \n This      is	 at test\n               and only a          test';
alert(xyz); // Should replace extra whitespace with 1 space or 1 new line if it contains new line

</script>

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
1 Comment
 
LVL 20

Accepted Solution

by:
Proculopsis earned 500 total points
ID: 34944200

//is this any good:

var xyz = 'Hello,     world. \n This      is       at test\n               and only a          test';

xyz = xyz.replace( /[ \t]*\n[ \t]*\n/g, "\n" );
xyz = xyz.replace( /[ \t]+/g, " " );

alert(xyz); // Should replace extra whitespace with 1 space or 1 new line if it contains new line
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

Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…
Suggested Courses
Course of the Month10 days, 11 hours left to enroll

628 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