I have:
var text = 'LTE CSSR (East xr) (301-LT_King_St_PC)'
and I want to split from (East xr) (301-LT_King_St_PC) only as follow :
var result = text.split('(')
result = (East xr) (301-LT_King_St_PC)
CodePudding user response:
You can use a regular expression with the match
function to get this done. Depending on how you want the result try one of the following:
var text = 'LTE CSSR (East xr) (301-LT_King_St_PC)'
console.log('one string:', text.match(/\(.*\)/)[0])
console.log('array of strings:', text.match(/\([^\)]*\)/g))
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
The first does what you seem to be asking for - a single output of everything between the first ( and the second ). It does this by searching for /\(.*\)/
which is a regex that says "everything in between the parentheses".
The second match
splits it into all parenthesised strings using /\([^\)]*\)/g
which is a regex that says "each parenthesised string" but the g
at the end says "all of those" so an array of each is given.
CodePudding user response:
You can do it using substring()
and indexOf()
:
var text = 'LTE CSSR (East xr) (301-LT_King_St_PC)';
var result = text.substring(text.indexOf('('));
console.log(result);
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
CodePudding user response:
You're quite close with your current method. One way you could try this is to use multiple characters within your split
var result = text.split(") (")
# Output -> result = ["(East xr", "301-LT_King_St_PC)"]
From here, some string manipulation could get rid of the brackets.
CodePudding user response:
Alternatively you can also use String.match
and join its result:
const text = 'LTE CSSR (East xr) (301-LT_King_St_PC)';
const cleaned = text.match(/\(. [^\)]\)/).join(` `);
console.log(cleaned);
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>