I am finding a specific word from a paragraph and want to make it bold. I have done below code but it's not working.
var text = /Lorem/ig;
$('div')
.filter(function() {
return text.test($(this).text())
}).wrap('<strong></strong>');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing
software like Aldus PageMaker including versions of Lorem Ipsum.</div>
Fiddle Demo
There is lil' plugin created by elclanrs
$.fn.wrapInTag = function(opts) {
var tag = opts.tag || 'strong'
, words = opts.words || []
, regex = RegExp(words.join('|'), 'gi') // case insensitive
, replacement = '<'+ tag +'>Lorem</'+ tag +'>';
return this.html(function() {
return $(this).text().replace(regex, replacement);
});};
// Usage
$('div').wrapInTag({
tag: 'strong',
words: ['Lorem'] //can be comma seprated like ['Lorem','ipsum']
});
Working Demo
You can use regex backreference:
var text=new RegExp('(Lorem)','ig');
$('div').html($('div').html().replace(text, "<strong>$1</strong>"));
Related
I have this function that divides given article by given letter count but it also split the words at the end of the lines, I would like to at hypen at the end of the line if the word is not completed/splitted.
var text = `Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.`
function divideTextByLetterCount(text, letterCount) {
let dividedText = "";
let currentLetterCount = 0;
for (let i = 0; i < text.length; i++) {
dividedText += text[i];
currentLetterCount++;
if (currentLetterCount === letterCount) {
if (dividedText.slice(-1) == ' ') {
dividedText = dividedText.slice(0, -1)
}
dividedText += "\n";
currentLetterCount = 0;
}
}
let dividedTextArr = dividedText.split('\n');
dividedTextArr.forEach((val, i) => {
if (val.slice(0, 1) == ' ') {
dividedTextArr[i] = val.slice(1);
}
});
return dividedTextArr.join('\n');
}
console.log(divideTextByLetterCount(text, 20));
So the output is,
Lorem Ipsum is simpl
y dummy text of the
printing and typeset
ting industry. Lorem
Ipsum has been the
industry's standard
dummy text ever sinc
e the 1500s, when an
unknown printer too
k a galley of type a
nd scrambled it to m
ake a type specimen
book. It has survive
d not only five cent
uries, but also the
leap into electronic
typesetting, remain
ing essentially unch
anged. It was popula
rised in the 1960s w
ith the release of L
etraset sheets conta
ining Lorem Ipsum pa
ssages, and more rec
ently with desktop p
ublishing software l
ike Aldus PageMaker
including versions o
f Lorem Ipsum.
But it should add hypen at the end of the lines which ends with uncompleted words like, simpl must be simpl- or sinc must be sinc-, how do I create that logic? thanks.
A much quicker and readable approach that also accomplishes the functionality you're looking for would be to use a regular expression. Match up to 20 characters, and put an optional capture group for a single non-space character in lookahead after the last character. If the capture group captures anything, you're in the middle of a word, and can add a -.
const text = `Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.`
function divideTextByLetterCount(text, letterCount) {
const pattern = new RegExp(`.{0,${letterCount}}(?=(\\S)?)`, 'g');
return text
.replace(
pattern,
(match, nextChar) => match.trim() + (!match.endsWith(' ') && nextChar ? '-' : '') + '\n'
);
}
console.log(divideTextByLetterCount(text, 20));
Example with 50 instead of 20:
const text = `Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.`
function divideTextByLetterCount(text, letterCount) {
const pattern = new RegExp(`.{0,${letterCount}}(?=(\\S)?)`, 'g');
return text
.replace(
pattern,
(match, nextChar) => match.trim() + (!match.endsWith(' ') && nextChar ? '-' : '') + '\n'
);
}
console.log(divideTextByLetterCount(text, 50));
I'm getting a console error when running the follow code:
<?php
$str = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.";
$str = wordwrap($str,30,'\n');
?>
document.write('test' + '\n');
document.write( '<?php echo $str;?>' + '\n' );
</script>
Console error: Uncaught SyntaxError: missing ) after argument list.
Where is the problem?
As #u_mulder noted you must escape quote sign ' as:
<?php
$str = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.";
$str = str_replace("'", "\'", wordwrap($str,30,'\n'));
?>
<script>
document.write('test' + '\n');
document.write( '<?php echo $str;?>' + '\n' );
</script>
Look PHP fiddle
I have string, for example
var string = 'This is my string. Lorem ipsum. Lorem ipsum.'.
This text, however, may be different.
My question is how, using JavaScript/jQuery, could I make a new line after every dot in this string so that each sentence starts in a new line ?
Thanks for help.
string.split(".").join(".\n")
Probably.
Using regex:
var string = 'This is my string. Lorem ipsum. Lorem ipsum.'
console.log(string.replace(/\./g, '.\n'))
var string = 'This is my string. Lorem ipsum. Lorem ipsum.';
string.split(".").join("<br>");
this what you mean ?
i am new to Regex usage, and have been searching for some time for suitable regex to retrieve URLs from a paragraph of text.
The current regex I am using:
text.match(/(((ftp|https?):\/\/)(www\.)?|www\.)([\da-z-_\.]+)([a-z\.]{2,7})([\/\w\.-_\?\&]*)*\/?/g);
Returns 'www.mik' as a valid URL from a paragraph of text like '...my webpage is www.mikealbert.com...' and is unsuitable for my purposes.
--
So far, the following regex gives me the best result for matching URLs ('www.mik' is not matched, but 'www.mikealbert.com' is matched)
/(https:[/][/]|http:[/][/]|www.)[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?\/?([a-zA-Z0-9\-\._\?\,\'/\\\+&%\$#\=~])*$/.test("www.google.com");
However, it can only be used to match single URLs. How should I modify the above regex to return an array of matching URLs? I will also need the regex to handle urls with paths, such as www.facebook.com/abc123?apple=pie&blueberry=cake
Thanks for any help!
Remove dollar sing from end of regex
var regex = /(https:[/][/]|http:[/][/]|www.)[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?\/?([a-zA-Z0-9\-\._\?\,\'/\\\+&%\$#\=~])/g;
var input = "https://stackoverflow.com/ lorem ipsum dolor sit amet http://google.com dolor sit amet www.foo.com";
if(regex.test(input)) {
console.log(input.match(regex));
}
output
[ 'https://stackoverflow.com/',
'http://google.com',
'www.foo.com' ]
Let's say I have the following string:
[lorem]{lorem;ipsum;solor;sit;amet}[ipsum]<i>Lorem</i> ipsum <b>dolor</b> sit amet
What I want is an object that contains the following:
{lorem: "{lorem;ipsum;solor;sit;amet}", ipsum: "<i>Lorem</i> ipsum <b>dolor</b> sit amet"}
How would my regular expression look? Is there any way to get the inverse of this?
/\[\w+\]/g
Thanks...
Instead of creating the inverse of /\[\w+\]/g, just use .split():
var string = '[lorem]{lorem;ipsum;solor;sit;amet}[ipsum]<i>Lorem</i> ipsum <b>dolor</b> sit amet'
console.log(string.split(/\[\w+\]/));
Demo