The function of the jquery code I currently have is to detect text enclosed with parenthesis and it will serve as a text anchor to a link.
The problem is that parenthesis appears on the text anchor too. Is it possible to show only the text? Also if it's possible to change the parenthesis() into brackets []. thanks in advance.
$("body").html($("#wrapper").html().replace(/(\([^)]+\))/, "<a href='https://www.sample-url-here.com/'>$1</a>"));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="wrapper">text (link here)</div>
The outcome I need should be:
from text [link here] to text link here
What about this? (jsfiddle)
$("body").html($("#wrapper").html().replace(/\(([^)]+)\)/, "[<a href='https://www.sample-url-here.com/'>$1</a>]"));
This will output a link surrounded by square brackets, with only the link underlined.
The problem with you original regular expression was that it was looking for anything not parentheses, surrounded by anything that is parenthesis, and that whole expression was surrounded by capturing parentheses.
In other words, let's look at it like this:
(x)
We'll call those your capturing parentheses. x can later be selected with $1.
x = \([^)]+\)
See the problem? Your entire x was being captured.
EDIT
jsfiddle updated to not show brackets.
EDIT
How about this one? jsfiddle
Related
I have an HTML code that contain CSS code inside tag under the header tag. I want to use regex to extract all text in HTML, only pure text (between HTML tags ). I tried,
console.log(HTML_TEXT.replace(/(<([^>]+)>)/g, ""))
which replace every thing between <> by empty char, the problem is the CSS code inside STYLE tag is still there, so i want to know how to write the regular expression to remove CSS code inside tags.
How do I solve this problem?
This RegEx might help you to do so:
(\>)(.+)(<\/style>)
It creates a right boundary in a capturing group: (<\/style>)
It has a left boundary in another capturing group: (\>), which you can add additional boundaries to it, if you wish/necessary
Then, it has a no-boundary middle capturing group, (.+), where your target is located, and you can call it using $2 and replace it with an empty string, or otherwise.
I'm not so sure, did not test it, but your code might look like something similar to:
console.log(HTML_TEXT.replace(/(\>)(.+)(<\/style>)/g, '\\$1\\$3'))
This post explains how to do a string replace in JavaScript.
Edit:
Based on the comment, this RegEx might help you to filter your tags using $1:
(\<style type=\"text\/css\"\>)([\s\S]*)(\<\/style\>)
Using regex javascript or Jquery, how do i select a lone character or in the body element? We have noticed that there's a random "s" at the bottom of our site, after the footer. We have no idea where it came from -it's really bizarre because it's in the body element, but it's not tied to any child element. it's just floating there in between some script tags.
screenshot of the 's' after the footer
here is the 's' in the DOM in between scripts
It might be from a plugin - but in the meantime, we were hoping we could hide this until we find the culprit.
I'm trying to select the element first with this in the console, but I've had no success.
jQuery('body').filter( function(){ return /([s]$)/g.test( jQuery(this).text() ) } )
However, I can't select this element.. does anyone know how I can do this? Thanks so much
If the s is the the start of the string after the closing script tag and is the only text on that line, you could use a capturing group and use that group $1 in the replacement:
(<\/script>\s*\n)s$
( Capturing group
<\/script>\s\n Match closing script tag followed by 0+ times a whitespace char and a newline
) Close capturing group
s Match s
$ End of string
Regex demo
const s = `<script type="text/javascript"><\/script>
s`;
console.log(s.replace(/(<\/script>\s*\n)s$/, "$1"));
If your app is php, try to search something like this in the beginning of the php file.
This is a piece of code that will be just echoed, not executed.
s<?php
I'm trying to grab an element's HTML using jQuery and then post it to the server. I successfully grabbed it, but I am not able to remove the white space between the tags and the line breaks that are rendered by default. The HTML code grabbed is shown below:
<table><tbody><tr><th></th><th>1</th><th>2</th><th>3</th><th>4</th><th>5</th><th>6</th><th>7</th></tr>
<tr><th>2nd row</th><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr></tbody></table>
I would like to trim the spaces between the tags only. I've used this regular expression: str.replace(/\s+/g, ' ');. But that doesn't seem to work, any suggestions?
Currently, you are replacing all consecutive sequences of whitespace with a single space.
This is what you want:
str.replace(/>\s+</g, '><');
I need to add escape character(backslash - ) character at the end of each line to wrap the string.
Hi i have the following text in a div:
[http://www.google.com Google this link] some other random text [http://example.com and another website] but don't parse [this is not a link, leave me alone].
What I tried to do was to convert the links into normal html links. The Format is always like this, so it opens with a [ and then the url, followed by the link text and then a closing ]. But I only want to match links, not all text in square brackets.
I want to use the .match() function in javascript to do this task, but I wasn't able to figure out the regex expression (I only need the text parts that are links - the rest should be a simple split).
Any help would be apprechiated.
Why .match and not .replace?
string.replace(/\[(https?:\/\/[^\]\s]+)(?: ([^\]]*))?\]/g, "<a href='$1'>$2</a>");
I am trying to put some text along with quotes in a DIV like this
"All is well that ends well"
now the text is dynamically generated and I am using javascript font replacement plugin (CUFON) for quotes around the text, sometime ending quote drops down to next line because of word wrapping like this
"All is well that ends well
"
How can I prevent that? I couldn't find a way to prevent it. code looks something like that
$("#q1").html('<span class="blue_quotes">“ </span>');
$("#ct1").html($.trim(commentText)+'<span class="blue_quotes">” </span>');
I added span tags for quotes because I need different font and color for quotes
You can use escape sequence \ for such cases ..
But I prefer using unicodes for such cases..
If you want to use the first approach try this
$("#q1").html('<span class="blue_quotes"> \“ </span>');
Check FIDDLE