Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8308081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:41:26+00:00 2026-06-08T18:41:26+00:00

I need detect words that separate with space in a text. for example my

  • 0

I need detect words that separate with space in a text. for example my text is:

some parent +kid -control "human right" world

now I need to detect some, parent, world. (all words that don’t have + – ( ) < > before and after, and all words inside quotes must be discarded) so I write this regex with preg_match_all():

(?:^|[\s]+)((?:(?![\+\(\)\<\>\s\-\"]).)+)(?:[\s]+|$)

but it only detect some and world. how can I fix it?

EDIT

I need it for Javascript too. But it seems it doesn’t work for Javascript. how can I do it with javascript?

EDIT

I found a solution but it seems stupid way. what is your ideas?

$str = 'some parent +kid -control "my human right" world';
$words=array();
$quot=false;
$discard=false;
$word='';
for($i=0;$i<=strlen($str);$i++){
    $chr=substr($str,$i,1);
    if($chr=='"'){
        if($quot){
            $quot=false;
        }else{
            $quot=true;
        }
        continue;
    }
    if($quot)continue;
    if($chr==' '||$i==strlen($str)){
        if(strlen($word)&&!$discard)$words[]=$word;
        $discard=false;
        $word='';
        continue;
    }elseif(in_array($chr,array('+','-','(',')','<','>'))){
        $discard=true;
        continue;
    }
    $word.=$chr;
}
print_r($words);//Array ( [0] => some [1] => parent [2] => world ) 

EDIT
Final way for PHP (this is for multi-language queries) (special thanks to rubber boots):

$query='some parent +kid -control "my human right" world';
$result=array();
if(preg_match_all('/(?:"[^"]+")|(?:^|[\s])(?P<q>(?:(?![\+\(\)\<\>\s\-\"]).)+)/',$query,$match)){
    $result=array_filter($match['q'],'strlen');
}
print_r($result);// some,parent,world

Final way for javascript (this is for multi-language queries) (special thanks to rubber boots):

var query='some parent +kid -control "my human right" world';
var result=Array();
var tmp;
var patt=RegExp('(?:"[^"]+")|(?:(?:^|\\s)((?:(?![\\+\\(\\)\\<\\>\\s\\-\\"]).)+))', 'g');
while(tmp = patt.exec(query)){
    if(typeof(tmp[1])!=='undefined') result.push(tmp[1]);
}
alert(result);// some,parent,world
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T18:41:27+00:00Added an answer on June 8, 2026 at 6:41 pm

    If the following string is given:

     $t ='some parent +kid -control "human huhu right" world';
    

    it’s possible to extract words according to your specification with a rather simple expression too:

     $r = '/ (?:" [^"]+ ")? \s?
             (?<!\S) \b (\w+)
           /x';
     preg_match_all($r, $t, $matches);
    

    This results in:

    foreach($matches[1] as $m) echo $m . "\n";
    
    some
    parent
    world
    

    The technique used:

    The expr (?:" [^"]+ ")? consumes the quotes and their contents.


    Addendum: Javascript

    For Javascript, you need to use a slightly more complicated approach, Javascript has no lookbehind assertions, we fake them with (?:^|\\s) in front of an allowed word.

    This will work:

      var t = 'some parent +kid -control "human huhu right" world';
      var r = /(?:"[^"]+")?(?:^|\s)(\b\w+)/g;
      var a = [];
      while(m = r.exec(t)) a.push(m[1]);
    

    We use the same technique here – generate captured submatches in $1 for the words we need.

    The contents of the array a, (document.getElementById("myhtml").innerHTML = a;) will contain then:

    some,parent,world
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to detect words in text, i.e. I need to know which characters
I need re.findall to detect words that are followed by a = So it
I need to detect search engines that refers to my website. Since every search
I'm building an application that uses an UIScrollView. I need to detect when the
I need a library that can detect objects in an image (uses edge detection).
I've spent some time, but still have to solution. I need regular expression that
I need to detect if a user pastes text from the clipboard into a
I need to detect all the red pixels in an image using Java. What
I need to detect IE7 (and IE6) using the user agent string: I have
I need to detect android device orientation change without playing manually with sensor data,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.