/    Sign up×
Community /Pin to ProfileBookmark

string.replace w/ reg exp not replacing

I’m having a regular expressing problem. When I pass the following:

[quote]

This is a test path. Here is a new line

Here are a couple of asterisk tests:
**
And here **
within a line.

Finally, here is a bbcode
[vid=”flythrough”]

[/quote]

into the following javascript funtion

[code]
txtHelper.saveText= function(txt)
{
txt.replace(/n/g, “\n”);
txt.replace(/**/g, “[]”);
}
[/code]

it doesn’t change anything, neither the new lines into “n” or the asterisks into “[]”. I know I must be missing something simple.

TIA,
anw

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@scragarOct 18.2007 — [code=php]txtHelper.saveText= function(txt)
{
txt.replace(/n/g, "\n");
txt.replace(/**/g, "[]");
return txt
}[/code]
EDIT:

actualy, I think I should just point out that only objects are normaly passed by reference in javascript, and as far as I know the isn't a standard method of passing them by reference, so your edits to txt here has no change on the value of the variable you pass unless you do something like:[code=php]var x ="cheese";
x = covert(x);
function convert(){
return arguments[0].replace(/e/g, "a");
};[/code]
sorry I can't say much more of help.
Copy linkTweet thisAlerts:
@anwauthorOct 19.2007 — Ha! Yes. I went off and had a couple of pitchers of beer, and this epiphany occurred to me. It works for the brackets, and I think for the new lines. When I send

This is a test path. Here is a new line

Here are a couple of asterisk tests:

**

And here **
within a line.

Finally, here is a bbcode

[vid="flythrough"]
[/quote]

through this:
<i>
</i>txtHelper.saveText= function(txt)
{
var str, rstr;
str= txt.replace(/n/g, "\n");
rstr= str.replace(/**/g, "[]");
return rstr;
}

I get this

This is a test path. Here is a new linennHere are a couple of asterisk tests:n[]nAnd here [] within a line.nnFinally, here is a bbcoden[vid="flythrough"]n
[/quote]

So, I think it's working. Just got to get to the decoding and try it!

Thanks so much,

anw
Copy linkTweet thisAlerts:
@KorOct 19.2007 — [B]scragar[/B] better use the wrapping [ code ] [ /code], instead of [ php ][ /php ] when using Regular Expressions (or any code with escapes...)

/x/ will be
<i>
</i>/x/

but
[code=php]
/x/
[/code]
×

Success!

Help @anw spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...