Feb 4, 2011 10:33 pm
JwcarltonI 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 Honnenjwcarlton 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?
> 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
- Conditional String.replace regex
- Replace string
- String replace
- String Replace. Please, need help.
- Replace in String
- Trouble with string replace
- How to replace ^ with an other character in a string ?
- Re: How to replace ^ with an other character in a string ?
- Quickest way to search and replace in a query string?