The easiest way to cut a shortcode from content? [closed] - javascript

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 days ago.
Improve this question
I need to get the contents of the shortcode and the updated content (without it).
Now I'm doing this and My output:
"dolor" and "Lorem ipsum sit amet, consectetuer adipiscing elit."
Is this the best way? Is there a more elegant way?
let input = `Lorem ipsum [foo]dolor[/foo] sit amet, consectetuer adipiscing elit.`;
let bbValue = ``;
let newContent = ``;
let regExp = new RegExp(
'^' +
'(?<contentBefore>.*?)' +
'\\[foo\\]' +
'(?<fooValue>.*?)' +
'\\[\\/foo\\]' +
'(?<contentAfter>.*?)' +
'$',
'uis'
);
let groups = input.match(regExp).groups;
bbValue = groups.fooValue;
newContent = groups.contentBefore + groups.contentAfter;
console.log(bbValue);
console.log(newContent);

Related

replace char with regexp and map - js [duplicate]

This question already has answers here:
The .replace() method does change the string in place [duplicate]
(3 answers)
Closed 2 years ago.
(this question is the continuation of my previous question : "how to find private char utf8 in a text?")
I need to replace each match with its value (which is stored in a map) but I have tested several things, including this one (below) but it does not work. I probably don’t have the hindsight to find the solution. I’ve been on for an hour. I probably did a big mistake sorry for that.
Here's my code:
const regex = /[\uE000-\uF8FF\u{F0000}-\u{FFFFD}\u{100000}-\u{10FFFD}]/gu;
var map = new Map();
//set example values in the map
map.set("󰀁", 'something1');
map.set("󰀂", 'something2');
map.set("󰀃", 'something3');
map.set("󰀄", 'something4');
const str = "\u{f0001} lorem ipsum \u{f0002} dolor sit amet \u{f0003}\n consectetur adipiscing elit\u{f0004}\sed do eiusmod tempor</"; // my text
console.log(str);
var tab = str.match(regex).map(x => Array.from(x)
.map((v) => v.codePointAt(0).toString(10))
.map((hex) => "0000".substring(0, 4 - hex.length) + hex))
for(var i = 0 ; i < tab.length ; i++){
str.replace(/[\uE000-\uF8FF\u{F0000}-\u{FFFFD}\u{100000}-\u{10FFFD}]/u, map.get("&#"+tab[i][0]+";"));
}
console.log(str); //nothing change
strings are immutable, no method or operators will change them.
in your for loop you should reassign str to the result of str.replace, I think that's all 🙂
for(var i = 0 ; i < tab.length ; i++){
str = str.replace(/[\uE000-\uF8FF\u{F0000}-\u{FFFFD}\u{100000}-\u{10FFFD}]/u, map.get("&#"+tab[i][0]+";"));
}
note that you don't have the 'g' flag on this regex, not sure if this is intended
Edit: as Wiktor pointed out, you will have to switch your declaration from const to let

Replace a pattern with a value in javascript [duplicate]

This question already has an answer here:
javascript replace with submatch as array index
(1 answer)
Closed last year.
I'm new to JavaScript and I'm working on an application that have something like that in a string format
"lorem ipsum dolor {#variable#} sit amet {#variable2#}"
How to remove {# and #} and replace the word variable with value and for the second replaces the word variable2 and replaces it with value2
I really appreciate the help.
Thank you in advance
You could use the regex {#(.*?)#} to find the substrings you want to replace. Then, use a map object to get the corresponding value for the captured variable:
let str = `lorem ipsum dolor {#variable#} sit amet {#variable2#}`
let map = {
variable: "value1",
variable2: "value2",
}
let replaced = str.replace(/{#(.*?)#}/g, (m, p1) => map[p1])
console.log(replaced)
Regex demo

Is there a Regex expression to match every that is not in quotations

I'm working on a Regular Expression that looks at everything except information in quotations. An example would be:
Lorem ipsum "dolor" sit amet, "consectetur" adipiscing elit.
Would change to
Lorem ipsum sit amet, adipiscing elit
I've already tried
.*(?=".*").*
and
`[^"].*[^"]
neither worked so I'd appreciate help.
Heres a way ...
var str = `Lorem ipsum "dolor" sit amet, "consectetur" adipiscing elit.`
console.log(str.replace(/\"[a-z]{1,}\"/g, ''))
I would do something in the line of this. It handles the case where the sentence starts or ends with a quotation mark.
var str = '"Lorem" ipsum "dolor" sit amet, "consectetur", adipiscing "elit."';
var regExp = /(?:^|\W)(\".+?\")(?:\W|$)/g;
var res = str.replace(regExp, " ").trim();
console.log(res);
RegExp explained:
(?:^|\W) - non capturing group of either start of the string or just a space (technically a "non word character" - could be a punctuation, a bracket, etc)
(\".+?\") - anything between " (including the quotation marks themselves)
(?:\W|$) - same as the first one, but checking for the end of a string instead
g flag - check for all occurrences of a matching pattern
Try this , non-greedy (".*?") match two quotations and as few as possible between them.
var a ='Lorem ipsum "dolor" sit amet, "consectetur" adipiscing elit.';
console.log(a.replace(/".*?"/g,''));
I hope to help you :
/[,\w]+(?![^" ]*")/g
var str = 'Lorem ipsum "dolor" sit amet, "consectetur" adipiscing elit.' ;
var res = '' ;
var patt = /[,\w]+(?![^" ]*")/g ;
while ( (arr = patt.exec(str)) != null) { res += arr[0] + ' ' ; }
console.log( res ) ;

How to make character count with Javascript

How to correctly make a character count with JS.
My code seems is incorrect, what i'm doign wrong?
<p class="text">Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
var str = document.getElementsByClassName('text');
var res = str.innerHTML = str.value.length;
console.log(res);
This can work for you
var x = document.getElementById("text").innerHTML
var str = x.length;
alert("length is:" +str);
console.log(str);
<p id="text">Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
If you look at this definition of the function it returns an array. So you either want the length of the array str.length or to iterate through the array.
getElementsByClassName() returns an array-like collection of elements. Iterate over it and fetch the value:
var str = document.getElementsByClassName('text');
[].forEach.call(str, function(el){
var res = el.value.length;
el.innerHTML = res;
console.log(res);
});
I guess you want to get the length of the content of an element. For that you should be using id selector, not class selector, there can be many elements with the same class but not with the same id. So you should give it some id like: <p id='some_id' class='text'>
Now you can get it through:
document.getElementById("some_id").innerHTML.length
To avoid any leading or trailing spaces, and to get exact length of text, you can try this:
document.getElementById("some_id").innerHTML.trim().length

Remove from String (from Array) in Javascript [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Remove a word from a string
I have a simple string var mystr = "Lorem ipsum dolor sit amet, consectetur adipiscing elit", and I have an array var lipsums = new Array("dolor","consectetur","elit"); Now, I want a simple function which will remove any same word in the string.
So, in the above example, it should remove the words "dolor", "consectetur", and "elit" and my string mystr should be "Lorem ipsum sit amet, adipiscing"
This script should be in Javascript (no jQuery). Any help would be appreciated.
Loop over the array of words to remove, removing all occurances via split/join:
for (var i = 0; i < lipsums.length; i++) {
mystr = mystr.split(lipsums[i]).join('');
}
http://jsfiddle.net/9Rgzd/
You may also want to clean up your whitespace afterwards, which you can do with a regex:
// Note: don't do this in the loop!
mystr = mystr.replace(/\s{2,}/g, ' ');
http://jsfiddle.net/9Rgzd/1/
Like this:
for(i=0; i<lipsums.length; i++) {
mystr = mystr.replace(new RegExp(lipsums[i],"g"), "");
}
Add this AFTER the loop to remove double white space's:
mystr = mystr.replace(/\s{2,}/g, ' ');

Categories

Resources