Find text javascript
Find text javascript
I Have string random length
Ex:
sadsadsadsad(323213)dfsssds
sadsadsadsad(321)dfsssds
How can I find the values in brackets?.
Value random length
Thank for reading
Possible duplicate of Regular Expression to get a string between parentheses in Javascript
– l2aelba
Sep 10 '18 at 15:41
I not find regex. Thank you
– Lightrain125
Sep 10 '18 at 15:43
You may want to accept the Duplicate, the answers will give you everything you need.
– Luca Kiebel
Sep 10 '18 at 15:44
7 Answers
7
Try This:
var str = 'sadsadsadsad(321)dfss888s(120)ds';
str.match(/(d+)/g).map ( value => console.log( value.replace(/[)(]/g, '') ) );
Try regex /(.*)/
/(.*)/
function getValue(s)
var strs = s.match(/(.*?)/g);
if (strs == null)
return '';
return strs.map(str=>str.replace(/[()]/g, ''));
//return strs.join(' ,').replace(/[()]/g, '')
console.log(getValue('sadsadsadsad(323213)dfsssds(abc)'));
console.log(getValue('sadsad(_d_)sadsad(321)dfsssds'));
console.log(getValue('sadsad(33)sadsad(321dfsssds'));
Thank for help, adsdadsdas(123)adadadad(123431) I need find (123) and (123431) how to?
– Lightrain125
Sep 10 '18 at 15:59
@Lightrain125
adsdadsdas(123)adadadad(123431)
return 123123431
?– Hongarc
Sep 10 '18 at 16:08
adsdadsdas(123)adadadad(123431)
123123431
return 123,123123431
– Lightrain125
Sep 10 '18 at 16:14
Your mean is return Array or return string with
,
?– Hongarc
Sep 10 '18 at 16:15
,
You can use lastIndexOf
and substring
lastIndexOf
substring
var text = "sadsadsadsad(323213)dfsssds";
var newText = text.substring(text.lastIndexOf("(") + 1, text.lastIndexOf(")"));
console.log(newText)
Thank for help, adsdadsdas(123)adadadad(123431) I need find (123) and (123431) how to?
– Lightrain125
Sep 10 '18 at 15:50
You can use indexOf
and substring
indexOf
substring
let str = 'sadsadsadsad(321)dfsssds';
let getFirstIndex = str.indexOf('(');
let getSecondIndex = str.indexOf(')');
let subStr = str.substring(getFirstIndex + 1, getSecondIndex);
console.log(subStr)
Thank for help, adsdadsdas(123)adadadad(123431) I need find (123) and (123431) how to?
– Lightrain125
Sep 10 '18 at 15:51
var str = "sadsadsadsad(323213)dfsssds";
var val= str.match(/((.*?))/);
if (val)
console.log("found");
You can use Regex
var str = "sadsadsadsad(323213)dfsssds";
var val= str.match(/((.*?))/);
if (val)
console.log("found");
Thank for help, adsdadsdas(123)adadadad(123431) I need find (123) and (123431) how to?
– Lightrain125
Sep 10 '18 at 15:47
Please refer to this article: stackoverflow.com/questions/6323417/…
– Yuri
Sep 10 '18 at 15:50
Try by using the following code it will helps you if you have remove nested Parentheses example "a(bcdefghijkl(mno)p)q"
"a(bcdefghijkl(mno)p)q"
function removeParentheses(s)
let openPatterns = ;
for(let i=0; i < s.length; i++)
if(s[i] == '(')
openPatterns.push(i);
if(s[i] == ')')
const lastIndexOpen = openPatterns.pop();
const originalTxt = s.slice(lastIndexOpen ,i+1);
//here you have the raw text
const rawText = s.slice(lastIndexOpen+1,i);
//this code works to remove the parentesis
//s= s.replace(originalTxt, reverseTxt);
//i-=2;
return rawText;
You could catch the group within ()
using replace()
, and passing it to a callback function that push it into an array.
()
replace()
var str = 'sadsadsadsad(323213)dfsssds(1234)dfsssds(4567)dfsssds(abcf)';
var arr = ;
str.replace(/(([a-z0-9]*))+/gi, (a,b)=>arr.push(b));
console.log(arr);
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
What have you tried in order to find them? Have you tried regex?
– Luca Kiebel
Sep 10 '18 at 15:39