Solved

Convert PHP preg_replace code to JavaScript replace

Posted on 2011-02-21
1
506 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
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

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
Asp in script 6 36
jQuery Dialog Autoresize Bug 2 24
jquery tab header text 1 22
jQuery force form POST 7 44
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

840 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