hi all, actually i have a code that works fine except for 2 things…my code actually replace all words with hashtag (e.g. #example) and turn it into link like:
export default function () {
const regex = /#[^\s!@#$%^&*()=+.\/,\[{\]};:'"?><]+/g;
const p = this.$('.Post-body');
const baseurl = app.forum.attribute('baseUrl');
p.html = p.html(p.html().replace(regex, match => `<a href="${baseurl}/?q=${match}" class="hashlink" title="Find more post with this hashtag">${match}</a>`))
}
can you give an example of a string returned by p.html() which you are chaining .replace(...) with? can you also show your desired string after replacements, as well as what your replace code actually gives you currently?
The easiest solution that I can think up is using slice(1) in your replace callback.
const regex = /#[^\s!@#$%^&*()=+.\/,\[{\]};:'"?><]+/g;
"These are some sample #words".replace(regex, match => match.slice(1))
// output: "There are some sample words"
So using some combo of match.slice(1) and match should be what you need.
If I am understanding the question correctly that should get the job done for ya. I do agree with @gaac510 , in that the question as posted is a little confusing.
Humm… I see that. Now it is only searching the beginning of the string. My bad.
What about a negative look behind?
Something like
regex = /(?<!html)#[^\s!@#$%^&*()=+.\/,\[{\]};:'"?><]+/g
"some #words with a link https://docs.flarum.org/composer.html#regex-are-complicated".replace(regex, match => match.slice(1))
// output: "some words with a link https://docs.flarum.org/composer.html#regex-are-complicated"
I’m still not 100% sure what you are trying to do. Are you saying it’s something like:
// The complete string returned by `p.html()` before replacement:
"These are some sample #words";
// The complete string you want to get after replacement:
`These are some sample <a href="${baseurl}/?q=words" class="hashlink" title="Find more post with this hashtag">#words</a>`
As well as ignoring strings similar to the below:
// The complete string returned by `p.html()` before replacement:
"Here's a url: https://docs.flarum.org/#goals" // Part of a url, so ignore.
If the above sound about right here’s a modified version of your regex you can try in combination with @codyjamesbrooks’ suggested usage of slice():
It’s logic is similar to that of @codyjamesbrooks’s final suggestion; his would disregard a match if "#" is immediately preceded by "html"; mine would disregard a match if "#" is preceded by "http://" (or "https://") with any number of non-white spaces separating the two.
regexModified relies on JS’ implementation of regex where non-fixed length lookbehind assertions are allowed. regexModified may not work in other languages (e.g. Python, PHP, Java).
regexModified may not cover all situations, similar to what you have experienced with @codyjamesbrooks’ final suggestion. It’s definitely possible to make it more robust but you need to provide us with better explained context (what your project is like and what role the replacement routine in question plays in your project) and examples of edge cases. Until we are given these information we’d just be guessing your needs and not getting anywhere.