The forum of the forums
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Asking about get text and replace text in Javascript

3 posters

Go down

Asking about get text and replace text in Javascript Empty Asking about get text and replace text in Javascript

Post by SarkZKalie 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.
SarkZKalie
SarkZKalie
Support Moderator
Support Moderator

Male Posts : 1443
Reputation : 220
Language : English

https://rotavn.forumotion.com/

Back to top Go down

Asking about get text and replace text in Javascript Empty Re: Asking about get text and replace text in Javascript

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

Bump Very Happy


Asking about get text and replace text in Javascript Sarkzk10
SarkZKalie
SarkZKalie
Support Moderator
Support Moderator

Male Posts : 1443
Reputation : 220
Language : English

https://rotavn.forumotion.com/

Back to top Go down

Asking about get text and replace text in Javascript Empty Re: Asking about get text and replace text in Javascript

Post by SSYT 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");
SSYT
SSYT
Forumember

Male Posts : 77
Reputation : 15
Language : RO-10, EN-3, FR-1
Location : Romania

http://help.forumgratuit.ro/forum

Back to top Go down

Asking about get text and replace text in Javascript Empty Re: Asking about get text and replace text in Javascript

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

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

Male Posts : 1443
Reputation : 220
Language : English

https://rotavn.forumotion.com/

Back to top Go down

Asking about get text and replace text in Javascript Empty Re: Asking about get text and replace text in Javascript

Post by Luiz~ 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.
Luiz~
Luiz~
New Member

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

http://ajuda.forumeiros.com/

Back to top Go down

Back to top

- Similar topics

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