Is there an easy way to remove an element from a PHP array, such that it foreach ($array)
no longer includes that element?
I thought setting it to null would do it, but apparently not.
Is there an easy way to remove an element from a PHP array, such that it foreach ($array)
no longer includes that element?
I thought setting it to null would do it, but apparently not.
I want to have a Javascript function that uses regular expressions to add things to the borders of certain words in a text.
Example, I want to replace this:
The swift Indian bat happily ate thistle and kiwi. The stork played the saxophone...
... for the following, knowing that the keywords are swift , thistle and kiwi :
The <b>fast</b> Indian bat happily ate <b>thistle<b> and <b>kiwi</b>. The stork played the saxophone...
I'm programming in JavaScript and HTML , and I have a textArea
(textarea) , and a div
where I show that I resultado
have processed the content of the textArea
.
But what I want is to process the selected text . That is, he resultado
is:
Mark in bold and large size the selected part.
My code is the following:
function procesar_selección(texto)
{
return texto
}
function procesar_selección_html()
{
var texto = document.getElementById("texto_html").value
resultado.innerHTML = procesar_selección(texto)
}
textarea{
height: 100px;
width : 400px;
}
<div>Escriba el texto a procesar:</div>
<textarea
id = "texto_html"
onkeyup = "procesar_selección_html()"
onkeydown = "procesar_selección_html()" >
</textarea>
<div id="resultado" ></div>
I have to process a string of comma separated values containing triplets of values and translate at run time each triplet to different types according to its content, the input data would be similar to:
"1x2y3z,80r160g255b,48h30m50s,1x3z,255b,1h,..."
So each sub-string should be processed as follows:
1x2y3z
would be processed as Vector3
with x = 1
, y = 2
and z = 3
.80r160g255b
would be processed as Color
with r = 80
, g = 160
and b = 255
.48h30m50s
would be processed as Time
with h = 48
, m = 30
and s = 50
.The problem I run into is that each component is optional (although it always appears in the same order) so the following strings are also Vector3
, Color
and Time
correct:
1x3z
would be processed as Vector3
with x = 1
, y = 0
and z = 3
.255b
would be processed as Color
with r = 0
, g = 0
and b = 255
.1h
would be processed as Time
with h = 1
, m = 0
and s = 0
.What have I tried so far?
((?:\d+A)?(?:\d+B)?(?:\d+C)?)
The characters A
, B
and C
would be replaced by the correct letter in each case. This expression works fine except for the fact that it returns twice as many expected results (one for the searched string and another returns an empty string just after the first match), for example:
1h1m1s
two matches:
"1h1m1s"
.""
.11x50z
two matches:
"11x50z"
.""
.11111h
two matches:
"11111h"
.""
.I can't say I didn't expect it... after all an empty string matches the provided regular expression when all components are empty; so to fix this issue i tried the following:
((?:\d+[ABC]){1,3})
But with this expression, strings are captured with the wrong order or even with repeated elements:
1s1m1h
a match, it should not match (wrong order).11z50z
a match, it should not match (repeated components).1r1r1b
a match, it should not match (repeated components).So I made another try with a modified version of my first try:
^
to end $
of the string.^((?:\d+A)?(?:\d+B)?(?:\d+C)?)$
It works better than the first version but still matches empty strings, with the added disadvantage that I must first separate the string by each comma ( ,
) and pass the expression over each sub-string.
The attempt using Lookahead:
\b(?=[^,])(?=.)((?:\d+A)?(?:\d+B)?(?:\d+C)?)\b
Against the following chain:
"48h30m50s,1h,1h1m1s,11111h,1s1m1h,1h1h1h,1s,1m,1443s,adfank,12322134445688,48h"
The results are very good, it detects valid matches without adding false positives. Unfortunately, every time a string is found that doesn't match the expression, it adds an empty string just before the invalid string (finds ""
before "1s1m1h"
, "1h1h1h"
, "adfank"
and "12322134445688"
) so I've made one last try by modifying the lookahead condition:
\b(?=(?:\d+[ABC]){1,3})(?=.)((?:\d+A)?(?:\d+B)?(?:\d+C)?)\b
This expression removes empty strings found before any strings that do not match (?:\d+[ABC]){1,3})
(empty strings before "adfank"
and "12322134445688"
), but empty strings before "1s1m1h"
, "1h1h1h"
are still caught.
So my question is: Is there a regular expression that matches value triplets in a given order, without repetitions, with all optional components but composed of at least one element, and doesn't match empty strings?
The regex tool I'm using is <regex>
from C++11 .
Beyond knowing the difference between the JOINS
explicit and implicit ones, I want to know which ones are more efficient and performing?