JavaScript Forum

Regex question for String.replace; match whitespace OR nothing

Feb 4, 2011 10:33 pm
Jwcarlton

I have a section that may, or may not, include a whitespace. Like
this:

var string = "<b><p>Jason</p> </b>";

I'm wanting to do this:

string = string.replace(/<b><p>jason<\/p> <\/b>/gi, "Jason");

But the problem is that the whitespace between </p> and </b> might NOT
exist. I'm doing it like this, but I know that it's wrong because it
would find anything, not just a whitespace:

string = string.replace(/<b><p>jason<\/p>(.*)<\/b>/gi, "Jason");

I'm pretty sure that this isn't right, but it's the direction I was
going:

string = string.replace(/<b><p>jason<\/p>(\s*|(.)*?)<\/b>/gi,
"Jason");


What's the correct way to match a whitespace, or nothing?

Feb 5, 2011 9:39 am
Martin Honnen
Re: Regex question for String.replace; match whitespace OR nothing

jwcarlton wrote:
> I have a section that may, or may not, include a whitespace. Like
> this:
>
> var string = "<b><p>Jason</p> </b>";
>
> I'm wanting to do this:
>
> string = string.replace(/<b><p>jason<\/p> <\/b>/gi, "Jason");
>
> But the problem is that the whitespace between</p> and</b> might NOT
> exist. I'm doing it like this, but I know that it's wrong because it
> would find anything, not just a whitespace:
>
> string = string.replace(/<b><p>jason<\/p>(.*)<\/b>/gi, "Jason");
>
> I'm pretty sure that this isn't right, but it's the direction I was
> going:
>
> string = string.replace(/<b><p>jason<\/p>(\s*|(.)*?)<\/b>/gi,
> "Jason");
>
>
> What's the correct way to match a whitespace, or nothing?

Well if you expect a single space or no space then use
/<b><p>jason<\/p> ?<\/b>/gi
or if you expect any white space then use
/<b><p>jason<\/p>\s*<\/b>/gi
But as one of your samples already uses \s I am not sure I understand
what problem you face.




Previous Thread: XHR Problem on all Browsers -- except IE
Next Thread: Re: Cascading drop-down restructuring

Related Forum Topics
String.replace() with regex
I have this regex, which works as expected:

a = a.replace(/ itxtHarvested="(.*?)"/i, "");

I modified it to this:

a = a.replace(/\s*itxtHarvested="*(.*?)"*/i, "");

which I thought would be the same, but would make the /s and " optional. But, in practice, instead of just removing it, it...
Conditional String.replace regex
I have a block of text, and I'm wanting to do something like, "if THIS
is not preceded by THAT, then replace THAT with THIS + THAT.

Meaning, I have this text:

<DIV style=float:left><FONT color=#ffff80 size=-1 face="Arial,
Verdana, Helvetica, sans-serif"><P><B>


If it's NOT preceded...
Replace string


How do you

var test = "<JIIM><BEAM><JIIM2><BEAM2>";
var x1= "><";
var x2= ">\\r\\n<";
test = test..replace(x1,x2);

why it complains when doing teh replace?
is teh syntax correct?


String replace

i'm trying to do a simple replace

document.write( "H<ello".replace("e","a") );

it just prints "H"

if i remove the < in the source string, then i get "H<allo" which is
what i expect.

why does it fail with < in the source string.

confused!



String Replace. Please, need help.
Hello,

I am using "replace" as follows:

content = source.replace("{id}", options.id).replace("{title}", title);

However "source" might have or not have the strings "{id}" and "{title}".

When it does not have I get an error.

How can I solve this?

--

Thank You,
Miguel


Replace in String
Hello,

I have the following:

var original = "<span>name at email dot com</span>"
var at = "at";
var dot = "dot";

email = original.text().replace(new RegExp(' ' + at + ' ', 'gi'), '@').replace(new RegExp(' ' + dot + ' ', 'gi'), '.');

I need to find the at and dot var values...
Trouble with string replace
Hi,

I'm using the JQuery framework but this question is about the string
replace method. I'm trying to replace carriage returns "\n" with the
"<BR/>" tag. Sadly, the replace method I have below is not
working ...

$('.replaceLineBreaks').each(function() {
console.log("replacing "...
How to replace ^ with an other character in a string ?
hi

I am trying to replace the character ^ in a string with character %
using "replace" string method. I know that ^ is a special character
so it has to be backslashed. But it doesnt work.

example

<html>
<body>
<script type="text/javascript">

var s = "x^12+2";
var s2 =...
Re: How to replace ^ with an other character in a string ?
On Thursday, March 10, 2011 1:05:45 PM UTC-3, ast wrote:
> hi
>
> I am trying to replace the character ^ in a string with character %
> using "replace" string method. I know that ^ is a special character
> so it has to be backslashed. But it doesnt work.
>
> example
>
> <html>
>...
Quickest way to search and replace in a query string?
Hi,

Given the search or query string from a URL ...

var searchStr = location.search;

How would i write a function, replace(paramName, paramVal), to replace
the value in the query string belonging to the param name,
"paramName", or, if there is no "paramName" given in the query...