ref:
In javascript, how do you search an array for a substring match
The solution given here is generic unlike the solution 4556343#4556343, which requires a previous parse to identify a string with which to join()
, that is not a component of any of the array strings.
Also, in that code /!id-[^!]*/
is more correctly, /![^!]*id-[^!]*/
to suit the question parameters:
- "search an array ..." (of strings or numbers and not functions, arrays, objects, etc.)
- "for only part of the string to match " (match can be anywhere)
- "return the ... matched ... element" (singular, not ALL, as in "... the ... elementS")
- "with the full string" (include the quotes)
... NetScape / FireFox solutions (see below for a JSON
solution):
javascript: /* "one-liner" statement solution */
alert(
["x'!x'\"id-2",'\' "id-1 "', "item","thing","id-3-text","class" ] .
toSource() . match( new RegExp(
'[^\\\\]("([^"]|\\\\")*' + 'id-' + '([^"]|\\\\")*[^\\\\]")' ) ) [1]
);
or
javascript:
ID = 'id-' ;
QS = '([^"]|\\\\")*' ; /* only strings with escaped double quotes */
RE = '[^\\\\]("' +QS+ ID +QS+ '[^\\\\]")' ;/* escaper of escaper of escaper */
RE = new RegExp( RE ) ;
RA = ["x'!x'\"id-2",'\' "id-1 "', "item","thing","id-3-text","class" ] ;
alert(RA.toSource().match(RE)[1]) ;
displays "x'!x'\"id-2"
.
Perhaps raiding the array to find ALL matches is 'cleaner'.
/* literally (? backslash star escape quotes it!) not true, it has this one v */
javascript: /* purely functional - it has no ... =! */
RA = ["x'!x'\"id-2",'\' "id-1 "', "item","thing","id-3-text","class" ] ;
function findInRA(ra,id){
ra.unshift(void 0) ; /* cheat the [" */
return ra . toSource() . match( new RegExp(
'[^\\\\]"' + '([^"]|\\\\")*' + id + '([^"]|\\\\")*' + '[^\\\\]"' ,
'g' ) ) ;
}
alert( findInRA( RA, 'id-' ) . join('\n\n') ) ;
displays:
"x'!x'\"id-2"
"' \"id-1 \""
"id-3-text"
Using, JSON.stringify()
:
javascript: /* needs prefix cleaning */
RA = ["x'!x'\"id-2",'\' "id-1 "', "item","thing","id-3-text","class" ] ;
function findInRA(ra,id){
return JSON.stringify( ra ) . match( new RegExp(
'[^\\\\]"([^"]|\\\\")*' + id + '([^"]|\\\\")*[^\\\\]"' ,
'g' ) ) ;
}
alert( findInRA( RA, 'id-' ) . join('\n\n') ) ;
displays:
["x'!x'\"id-2"
,"' \"id-1 \""
,"id-3-text"
wrinkles:
- The "unescaped" global RegExp is
/[^\]"([^"]|\")*id-([^"]|\")*[^\]"/g
with the \
to be found literally. In order for ([^"]|\")*
to match strings with all "
's escaped as \"
, the \
itself must be escaped as ([^"]|\\")*
. When this is referenced as a string to be concatenated with id-
, each \
must again be escaped, hence ([^"]|\\\\")*
!
- A search
ID
that has a \
, *
, "
, ..., must also be escaped via .toSource()
or JSON
or ... .
null
search results should return ''
(or ""
as in an EMPTY string which contains NO "
!) or []
(for all search).
- If the search results are to be incorporated into the program code for further processing, then
eval()
is necessary, like eval('['+findInRA(RA,ID).join(',')+']')
.
--------------------------------------------------------------------------------
Digression:
Raids and escapes? Is this code conflicted?
The semiotics, syntax and semantics of /* it has no ... =! */
emphatically elucidates the escaping of quoted literals conflict.
Does "no =" mean:
- "no '=' sign" as in
javascript:alert('\x3D')
(Not! Run it and see that there is!),
- "no javascript statement with the assignment operator",
- "no equal" as in "nothing identical in any other code" (previous code solutions demonstrate there are functional equivalents),
- ...
Quoting on another level can also be done with the immediate mode javascript protocol URI's below. (// commentaries end on a new line (aka nl, ctrl-J, LineFeed, ASCII decimal 10, octal 12, hex A) which requires quoting since inserting a nl, by pressing the Return key, invokes the URI.)
javascript:/* a comment */ alert('visible') ;
javascript:// a comment ; alert( 'not' ) this is all comment %0A;
javascript:// a comment %0A alert('visible but %\0A is wrong ') // X %0A
javascript:// a comment %0A alert('visible but %'+'0A is a pain to type') ;
Note: Cut and paste any of the javascript:
lines as an immediate mode URI (at least, at most?, in FireFox) to use first javascript:
as a URI scheme or protocol and the rest as JS labels.
results = arr.filter(function (v) {return /id-/.test(v)});
. And then you could do whatever you want with the results.windowArray.find((string) => string.startsWith("id-"))
if you’re only interested inid-
as a possible prefix.