Asking about get text and replace text in Javascript

Go down

Asking about get text and replace text in Javascript

Post by SarkZKalie on January 11th 2018, 3:01 pm

Hello

Im still learning a little Javascript language but after failed to install this add-on to my site, I came up with an idea.

For example I have a hyperlink text
Code:
<a id="xyz" href="/profile?friend='ABC'&mode=editprofile&page_profil=friendsfoes"><img src="/smiley.png" /></a>
I wanna write a script that can get and return text from link, then replace 'ABC' by text I just get.
Code:
var getname = '{postrow.displayed.POSTER_NAME}'; // <a href="http://domain.com/u1">Volunteer</a>
var gettext = getname.text; // return Volunteer
var result = replace(/'ABC'/gi, "gettext");

Why do I must use a variable?
- Cuz '{postrow.displayed.POSTER_NAME}' will only work in viewtopic_body template and return a text with link included itself. Example : https://help.forumotion.com/u1, not a simple text.

So my complete hyperlink html code will be like this
Code:
<a id="xyz" href="/profile?friend=Volunteer&mode=editprofile&page_profil=friendsfoes"><img src="/smiley.png" /></a>

I would appreciate any help.
Thank you for reading.
avatar
SarkZKalie
Forumember

Male Posts : 756
Reputation : 111
Language : English

http://rotavn.forumotion.com

Back to top Go down

Re: Asking about get text and replace text in Javascript

Post by SarkZKalie on January 13th 2018, 5:51 am

Bump Very Happy
avatar
SarkZKalie
Forumember

Male Posts : 756
Reputation : 111
Language : English

http://rotavn.forumotion.com

Back to top Go down

Re: Asking about get text and replace text in Javascript

Post by SSYT on January 13th 2018, 3:01 pm

Code:
var getname = '{postrow.displayed.POSTER_NAME}';
var gettext = getname;
var result = gettext.replace(/\<a href="(.*?)"\>(.*?)\<\/a\>/gi, "$1 = link | $2 = username");
avatar
SSYT
Forumember

Male Posts : 71
Reputation : 12
Language : RO-10, EN-3, FR-1
Location : Romania

http://help.forumgratuit.ro/forum

Back to top Go down

Re: Asking about get text and replace text in Javascript

Post by SarkZKalie on January 14th 2018, 7:06 pm

Thank you for your quick respond! Could you tell me the way how this script works?
avatar
SarkZKalie
Forumember

Male Posts : 756
Reputation : 111
Language : English

http://rotavn.forumotion.com

Back to top Go down

Re: Asking about get text and replace text in Javascript

Post by Luiz~ on January 27th 2018, 1:07 pm

-> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace
-> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp

He used the string's replace method with regular expressions.

---

In the example below, I will replace all digits with
Code:
 [D] 
:
Code:
var str = 'I can count from 1 to 10.';

str = str.replace(/\d/g, ' [D] ');

console.log(str); // I can count from  [D]  to  [D]  [D] .

... And so on:
Code:
var str = 'I can count from  [D]  to  [D]  [D] .';

str = str.replace(/\s\[D\]\s/gi, function () {
  return Math.floor(Math.random() * 20);
});

console.log(str); // I can count from 14 to 193.
                  // The numbers are ramdom!

To learn more, read the regular expression documentation.
avatar
Luiz~
New Member

Male Posts : 17
Reputation : 8
Language : PT
Location : Brazil

http://ajuda.forumeiros.com/

Back to top Go down

Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum