Remove line of hyphens - javascript

I'm trying to remove redundant dashed lines from a client's blog site because its erratic lengths are butting heads with the responsive design. Example: ---------------------------------
I created a jquery plug-in to remove individual lines from one post:
$('p:contains(——————————————————————————————-),
p:contains(———————————————————————–),
p:contains(————————————————————————————-),
p:contains(———————————————————————————),
p:contains(——————————————————————–),
p:contains(————————————————————-),
p:contains(————————————————————————————————),
p:contains(—————————————————————————),
p:contains(————————————————————),
p:contains(———————————————————————————–),
p:contains(——————————————————————————-)').each(function() {
$(this).remove();
});
But even this method is redundant lol. I tried rewriting like so using regex:
$('p:contains(----)').each(function(text) {
var hyphen = text.replace(/\u00AD/g,'');
return hyphen;
});
It didn't work and I've since been stuck on it for an hour. If anyone could give me a push in the right direction I would greatly appreciate it. Thanks!

Giving the jQuery .text() method a function is the way to perform a direct replacement of an element's text. The function receives the old text as its second argument, and whatever it returns will be used as the replacement.
$("p:contains(---)").text(function(i, text) {
return text.replace(/-{3,}/g, '');
});

Related

Scan dom/webpage after certain pattern and get domtag

I write a small chrome extension which includes adding buttons add specific positions.
These positions are mostly random and can't be determined with normal css/jQuery selectors.
I need to scan the whole page for a certain text pattern (regex).
After I found matches I need to get the dom tag where the text is in.
I tried parsing the whole source with body.innerHtml but I cant get the tag obj afterwards.
Any ideas on how to accomplish such a task are highly appreciated!
Sounds like you could use :contains() for this.
$(":contains('Your Text')")
For finding text using a regular expression use .filter()
var regex = new RegExp("Your Text");
$("*").filter(function () {
return regex.test($(this).text());
});

Getting current sentence using Office Javascript API

Using the Office Javascript API, I want to be able to select the current sentence in Word. By current sentence, I mean identifying where the caret position is, then iterating from that position to get the full sentence.
However, looking at the available API calls (such as getSelectedData), this does not seem possible, as there doesn't seem to be a way to get the current caret position.
I know when creating a C# project, you could use 'Microsoft.Office.Interop.Word.Range' and 'Selection.Range' to get a range, which you could use as a caret position.
Am I wrong or can you not get the caret position using the Javascript API?
Late but maybe still helpful for someone:
You can achieve this by combining getSelection() and getTextRanges([separators])on a document - this expands the current selection in both directions, until any of the characters in the list of separators is found.
Documentation for getTextRanges: https://dev.office.com/reference/add-ins/word/range
Example (typescript):
Word.run(context => {
let sentences = context.document.getSelection().getTextRanges(['\n', '.', '?'], false);
context.load(sentences);
context.sync().then(() => {
console.log(sentences.items);
}
return context.sync();
}

Highlight Text plugin breaks when using with array string

I am trying to use jQuery Based text highlight plugin, it works for single word highlight but breaks when i pass array, My syntax seems to be correct as the the documentation http://bartaz.github.io/sandbox.js/jquery.highlight.html
Example: http://jsfiddle.net/YyAXP/6/
//$('#article').highlight("me");
$("#article").highlight(["me","highlight","plugin"]);
I need to pass several keywords to this function so that it highlight all of them.
Solved:
It seems script had bug which was resolved use the following fiddle with complete script for array based search highlight script source
Fiddle: http://fiddle.jshell.net/ogyyvvog/2/
It gets error when running your code
pat.toUpperCase is not a function
pat should be array, maybe you can fix it in this way?
return this.length && pat && pat.length ? this.each(function () {
for(var i=0;i<pat.length;i++)
innerHighlight(this, pat[i].toUpperCase());
}) : this;
jsfiddle
Declaration syntax is correct
$("#article").highlight(["me","highlight","plugin"]);
You just need to correctly include the plugin in your jsfiddle. Do not include tag script, use instead "External Resources" menu... check updated demo
You can use my highlighting plugin jQuiteLight, that can easily work with both arrays and also regular expressions.
// for strings
$(".element").mark("query here");
// for RegExp
$(".element").mark(new RegExp(/query reg[a-zA-Z]+/));
// for array
$(".element").mark(["string query", new RegExp(/query arr[a-z]+/)]);

How can I make jQuery location.search work more efficiently with a regex?

So, I've got some jQuery code that's hiding the ".toggle-featured" div (Featured section) on any page other than the blog index pages of this site (in development), for example: http://sabq-dev.squarespace.com/experts/
It works fine, but I know there's got to be a better way to achieve the same result with less redundant js.
Here's an example of what I've got currently:
(function($){
if(location.search == "?category=Creativity-Intelligence"){
$('.toggle-featured').hide();
}
})(jQuery);
Then I'm repeating this for each category query (10 more which constitute the links in the Filter Categories navigation bar and are the same for every blog section), when I believe that all I really need is the correct regex to have the script find any query prepended with "?=category" and then hide the ".toggle-featured" div. Hopefully that makes sense?
Thus far, all my reading up on regular expressions has gotten me nowhere and I'm sure it's something relatively simple, but for the life of me I can't seem to figure it out.
Any ideas would be greatly appreciated
Thanks!
If all you need to do is find a set string, you can do this:
var s = location.search;
if(s.indexOf('?category=') != -1) {
$('.toggle-featured').hide();
Use
if ( /\?category=/.test(location.search) ) {
It checks whether ?category= is a substring of location.search. For this particular test, you needn't resort to a regex,
if ( location.search.indexOf("?category=") >= 0) {
Would work as well.
The backslash is necessary to escape the special meaning of ? in regex Syntax (zero or one occurrence).

how do I replace one word found many places under one class with javascript?

I'm writing a chrome extension to redesign the tumblr dashboard for a project.
This is a small thing but has been very frustrating. I want to get rid of the word "notes" after the number of notes bellow each post.
The string is under a class .note_link_current so I tried this
var textNotes = $('.note_link_current').text();
textNotes = textNotes.replace('notes', ' ');
$('.note_link_current').text(textNotes);
What seems to happen is that I get a combined string of all the notes of all the posts.. How do I get it so it affects all of them individually?
Right now I'm getting something like this:
251
63 notes441 notes74 notes851 notes49,263 notes2,561 notes142 notes1 note651 notes
Any help would be greatly appreciated!
What #FelixKling said, but with more explanation:
A jQuery selector like $('.note_link_current') will return an array of nodes. You're trying to make an operation on this array like it was one node.
Instead, what you should do is invoke .each(), to perform an operation on every element.
$('.note_link_current').each( function(index) {
// Inside this function, "this" refers to the (DOM) element selected
var text = $(this).text();
/* ... */
});

Categories